Studio5000 Convert HEX to STRING

SVJA

Member
Join Date
Jun 2022
Location
Västerås
Posts
5
Hi

I need to convert a DINT with HEX value e.g A0F15663 to a string with the same value 'A0F15663'.

Any tips of good instructions to use ?

Help appreciated
 
Hi

I need to convert a DINT with HEX value e.g A0F15663 to a string with the same value 'A0F15663'.

Any tips of good instructions to use ?

Help appreciated

Hi I was looking at the instruction help in Studio V. 32 and there are ASCII conversion instructions. Click the drop down and select "Dint to String". You would probably have to convert the hex to a Dint but maybe not. I remember doing some of this converting a hex to decimal in alarm logic for a job I did last summer however I've misplaced the copy of the program. Hope this helps.
 
Last edited:
Hi I was looking at the instruction help in Studio V. 32 and there are ASCII conversion instructions. Click the drop down and select "Dint to String". You would probably have to convert the hex to a Dint but maybe not. I remember doing some of this converting a hex to decimal in alarm logic for a job I did last summer however I've misplaced the copy of the program. Hope this helps.

I have tried DTOS instruction and it unfortunatly only returns the decimal value to the string, I have not found a way to force it to report HEX.

Thanks for the reply.
 
I have tried DTOS instruction and it unfortunatly only returns the decimal value to the string, I have not found a way to force it to report HEX.

Thanks for the reply.

I ended up creating my own AOI for converting a DINT to a HEXSTRING by extracting every 4 bit data value and converting it to a HEX value e.g if the decimal value is 10 from the 4 bits i copy 16#41 which is A in a string and so on.

A dint can contain 8 HEX values so i start from Bit 4-7 for first char then bit 0-3 for second, Bit 12-15 for third, bit 8-11 for fourth and so on.
 
Yup, that is the way to do it!

Maybe you did this, but we can put the sixteen hexadecimal digits into an array of 1-character strings ["0" "1" "2" "3" "4" "5" "6" "7" "8" "9" "A" "B" "C" "D" "E" "F"] and CONCAT them one at a time using the 4-bit values as the index as we extract them. So the sequence would be summat like

MOV dint_tag tmp_dint_tag CLR string_tag.LEN 0

AND tmp_dint_tag 15 tmp_4_dint_tag CONCAT string_array[tmp_4_dint_tag] string_tag string_tag SUB tmp_dint_tag tmp_4_dint_tag tmp_dint_tag DIV tmp_dint_tag 16 tmp_dint_tag


AND tmp_dint_tag 15 tmp_4_dint_tag CONCAT string_array[tmp_4_dint_tag] string_tag string_tag SUB tmp_dint_tag tmp_4_dint_tag tmp_dint_tag DIV tmp_dint_tag 16 tmp_dint_tag


AND tmp_dint_tag 15 tmp_4_dint_tag CONCAT string_array[tmp_4_dint_tag] string_tag string_tag SUB tmp_dint_tag tmp_4_dint_tag tmp_dint_tag DIV tmp_dint_tag 16 tmp_dint_tag


AND tmp_dint_tag 15 tmp_4_dint_tag CONCAT string_array[tmp_4_dint_tag] string_tag string_tag SUB tmp_dint_tag tmp_4_dint_tag tmp_dint_tag DIV tmp_dint_tag 16 tmp_dint_tag


AND tmp_dint_tag 15 tmp_4_dint_tag CONCAT string_array[tmp_4_dint_tag] string_tag string_tag SUB tmp_dint_tag tmp_4_dint_tag tmp_dint_tag DIV tmp_dint_tag 16 tmp_dint_tag


AND tmp_dint_tag 15 tmp_4_dint_tag CONCAT string_array[tmp_4_dint_tag] string_tag string_tag SUB tmp_dint_tag tmp_4_dint_tag tmp_dint_tag DIV tmp_dint_tag 16 tmp_dint_tag


AND tmp_dint_tag 15 tmp_4_dint_tag CONCAT string_array[tmp_4_dint_tag] string_tag string_tag SUB tmp_dint_tag tmp_4_dint_tag tmp_dint_tag DIV tmp_dint_tag 16 tmp_dint_tag


AND tmp_dint_tag 15 tmp_4_dint_tag CONCAT string_array[tmp_4_dint_tag] string_tag string_tag SUB tmp_dint_tag tmp_4_dint_tag tmp_dint_tag DIV tmp_dint_tag 16 tmp_dint_tag



It could be done in a loop but this is just as good.

I suspect OP used an array of SINTs [48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 65, 66, 67, 68, 69, 70] and MOVed them into each position of the 8-character string, instead of the CONCAT.
 
Last edited:
This is definitely the sort of task that's best done at the HMI or SCADA level, but failing that, an AOI like you ended up creating would be my fallback plan.
 
I did something similar to this today and remembered this post. This is based on Rockwell terminology. Adjust as needed.

AND your DINT with 15 to Temp (or other means to get working with the four least significant bits for 0 to F)

LIM 0 - 9 Temp, ADD Temp & 48 to String.Data[0] (ASCII 0 - 9 are 48 - 57)
LIM 10 - 15 Temp, ADD Temp & 55 to String.Data[0] (ASCII A - F are 65 - 70)

Shift your DINT bits right 4 positions with BTD or other method to get the next four bits and repeat for String.Data[1]

Repeat to String.Data[7]
MOV 8 to String.LEN

…or get fancier for varying lengths.
 
Last edited:
Back to this today I see that my previous logic puts characters out of order, at least for what I’m doing. The upper four bits of each SINT are the first character and the lower four are the second character.

I created an AOI to convert the SINT[6] result from a get MAC MSG in Rockwell to a 12 character string. OP used ladder, but here’s my logic: (Temp and i are local DINTs)


DestString.LEN := 12;

for i := 0 to 5 do
// Lower four bits if incoming SINT to second ASCII character
Temp := Source & 15;
if Temp < 10 then
DestString.DATA[2*i+1] := Temp + 48;​
else DestString.DATA[2*i+1] := Temp + 55;
end_if;

// Upper four bits if incoming SINT to first ASCII character
Temp := (Source & 240) / 16;
if Temp < 10 then
DestString.DATA[2*i] := Temp + 48;​
else DestString.DATA[2*i] := Temp + 55;
end_if;

end_for;



Composed this on iPad where Apple doesn’t think I have any use for a tab key. INDENT adds a line I didn’t really want.
 
Nice.

SINT is a Signed INTeger, isn't it?

I don't know how OP's ST handles promotion, but it might make that 240 be -16, or it might not matter.
 
Thanks. In this case, Short INTeger, 8 bit signed. After the top four SINT bits get masked, it’s apparently handled as a DINT before /16 shifts them. -120 Decimal in a SINT correctly converts to string 88. A few hours after posting that I thought it would be nice to add commas to a longer string. Here’s the current AOI:

AE4D2CBD-A07A-4412-A00B-824D1C06A735.jpeg
 
Last edited:

Similar Topics

Hi all, I have a question on how to hande data that i read from a sensor (type IFM) trough IO-Link (1734-4IOL) I get 2 SINT's (SINT[0] and...
Replies
2
Views
662
Customer has a program running in a 1769-L19ER-BB1B FW ver. 30 controller and bought a used machine with a 1769-L23E-QBFC1. Can we convert the...
Replies
4
Views
1,366
I have a Structure in my project which has a few nested UDTs. They are ultimately of type DINT. What I would like to do is copy the values out of...
Replies
5
Views
2,216
Hi Hope you all are doing well. Iam working on a project with some AOI. I also hate no online edits... lol. My problem occurs when I use a UDT...
Replies
1
Views
40
I am not sure if this is possible but if there is a way, you guys would be the ones to know. I am currently working on a project where we are...
Replies
7
Views
184
Back
Top Bottom