Dword to char

Asbuild

Member
Join Date
Jun 2005
Location
Gent
Posts
12
Hallo,

Can sombody help me to convert a DWORD to a string or Chars.
I have no problems with DINT to string with the help on this forum.

Thanks.
Jacques
 
Just a hint, Jacques...

Wouldn't you have to break the DWORD into pieces that could then be displayed as characters?
 
Jacques, what you you trying to do? Convert a number in hex (BCD in this case) to a string? I ask this because you mentioned a prior dint conversion, so I assume you are converting numbers.

Anyway, I'll take a stab at it. Let's say you have a four digit number in hex: 9876. Looking at the doubleword in the Siemens PLC world, you would have DW#16#09080706. Now, if I OR that doubleword with DW#16#30303030, it will be converted to characters: DW#16#39383736. If I monitored that doubleword in the CHAR format, I would see '9876'.

You can also reverse engineer this (I'm assuming you are using Siemens, but it would work with any other system) by entering some simple code:

Code:
	 L	 '9876'
	 L	 DW#16#30303030
	 -D	
	 T	 MD	50

You can go the other way and see what's going on:

Code:
	 L	 MD	50
	 L	 DW#16#30303030
	 OD	
	 T	 MD	40

MD40 will contain the four character string '9876'.

If you add some more details, we can probably help more.
 
Hallo,

Thanks for al the help.

I wordk with a Siemens PLC S7-313.
The convertion of the DWORD to char i need it for a machine that counts pices of materials.
It is placed in DB40.
I need to convert the decimal value of the DWORD en place it in DB41 as a string to communicate with a display.

String in DB41 = "_001Z00_02GA********_04"

Where the * are the char's of the 8 digit counter value in chars.

Thanks
Jacques
 
Ok, then if it is an 8 digit number, you can use the method I just described above. You could handle it either as two doublewords or eight bytes.
 
Ok but in your example you use hex numbers.
'9876' = 39030 dec
Now de string must be "39030" on the display.

Jacques
 
Here's my implementation. If the number is less than or equal to 9,999,999 you can use the Siemens DTB instruction to convert the number to BCD and then split the nibbles up and convert each one to ascii. If the number is greater than 9,999,999 you find the number of 10,000,000 units and then subtract this number of units from the original number to again allow you to use the DTB instruction. You have to manually convert the number of 10,000,000 units to form the top digit. Note this will only work for numbers <= 99,999,999
Code:
//dwv:dword
//wNumber:dword
//iTopDigit:int
//wz:dword
//Digit:array[1..8] of char
	  L	 L#87654321				  //test number
	  T	 #dwNumber
	  L	 #dwNumber
	  L	 L#9999999				   //if greater than 9,999,999 reduce it down before converting to bcd
	  >D	
	  JC	tob
	  DTB							   //else convert to bcd
	  T	 #dwv						//and make top digit 0
	  L	 0
	  T	 #iTopDigit
	  JU	pas						 //convert to chars
tob:  NOP   0
	  TAK   
	  L	 L#10000000				  //find out number of 10,000,000 in number
	  /D	
	  T	 #iTopDigit				  //to form top digit
	  L	 L#10000000
	  *D	
	  L	 #dwNumber
	  TAK   
	  -D								//now take off number of 10,000,000 so we can use BCD convert
	  DTB   
	  T	 #dwv
pas:  NOP   0
	  L	 #dwv						//now get nibbles of number and form character result
	  T	 #wz
	  LAR1  P##wz
	  L	 B [AR1,P#0.0]
	  AW	W#16#F
	  +	 48
	  T	 #Digit[7]
	  L	 #iTopDigit
	  ITB   
	  AW	W#16#F
	  +	 48
	  T	 #Digit[8]
	  L	 B [AR1,P#1.0]
	  AW	W#16#F
	  +	 48
	  T	 #Digit[5]
	  L	 B [AR1,P#1.0]
	  SRW   4
	  AW	W#16#F
	  +	 48
	  T	 #Digit[6]
	  L	 B [AR1,P#2.0]
	  AW	W#16#F
	  +	 48
	  T	 #Digit[3]
	  L	 B [AR1,P#2.0]
	  SRW   4
	  AW	W#16#F
	  +	 48
	  T	 #Digit[4]
	  L	 B [AR1,P#3.0]
	  AW	W#16#F
	  +	 48
	  T	 #Digit[1]
	  L	 B [AR1,P#3.0]
	  SRW   4
	  AW	W#16#F
	  +	 48
	  T	 #Digit[2]
 
Peter said...

"Terry, which byte do you copy? Doesn't it depend on whether the DWORD is in big or little endian format? Does it make a difference?"

Geez... Of course Peter! But then, shouldn't every programmer KNOW the nature of the data in any PLC he is working with? (ie., Data sizes and Big-Endian vs. Little-Endian)

Don't you? Why should I expect less from other "programmers"?

As I said... "Just a hint..."
 

Similar Topics

I'm trying to set alarm as DWord where each bit is individual alarm. So far I have tryed with DWord.0, DWord.1, DWord.2, etc. DWord.L0...
Replies
8
Views
820
Hello, I am using Omron PLC: NX1P2 Software:-Sysmac Studio Plc in which I have to read data from an energy meter over Modbus RTU The data that I...
Replies
3
Views
876
Hello, I am provided with PLC tags with PLC_UDT datatype via symbolic connection. The structure of PLC_UDT (DI_hmi) is: DI_hmi.stat (UDINT)...
Replies
4
Views
1,414
This feels like a simple, elementary question, but I am a Siemens novice and know there are experts here. My S7-1500 project (TIA Portal v17) has...
Replies
10
Views
2,682
I am needing to convert the low DWORD of a timestamp into a LREAL. The DWORD is a 32bit unsigned number. In TwinCat2, I keep getting a negative...
Replies
3
Views
1,677
Back
Top Bottom