I have converted an array of bytes into a string. Worked like a charm with the oscat function btw.
Now i have to put it into hex-values. Any way to create a string with hex values?
Thanks
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Anonymous
-
2017-09-08
Originally created by: scott_cunningham
You can make your own solution by using two offsets. Look at each byte. If it is >=10 then add 55 to it (A is ascii 65). For smaller number, add ascii offset for 0 - sorry don't remember if it is 40 or 42). Examine bytes from highest to lowest and con at your chars.
There is probably a prebuilt function somewhere, but it may take you longer searching for it than building it.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
scott_cunningham hat geschrieben:
You can make your own solution by using two offsets. Look at each byte. If it is >=10 then add 55 to it (A is ascii 65). For smaller number, add ascii offset for 0 - sorry don't remember if it is 40 or 42). Examine bytes from highest to lowest and con at your chars.
There is probably a prebuilt function somewhere, but it may take you longer searching for it than building it.
OK that's great but still facing a major problem for me.
Let's say i got value 58 in one byte. Then i want it displayed as 3A. (Hex is 4 bits). And i don't see any solution in it then
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Anonymous
-
2017-09-11
Originally created by: scott_cunningham
58 = 0011 1010 in binary. Hex uses four bits (nibble). You have to look at the upper four bits (3), then the lower four bits (A).
A WORD uses two bytes (four nibbles) and can have a number from 0000 to FFFF hex.
A BYTE uses one byte (two nibbles) and can have a number from 00 to FF hex.
Here is a crude function to convert a BYTE to a two char string. It is up to you to expand it for longer variable types...
FUNCTION BYTE_TO_HEX_STRING : STRING
VAR_INPUT
Input : BYTE;
END_VAR
VAR
Temp : BYTE;
Chr : BYTE;
Ans : STRING;
END_VAR
scott_cunningham hat geschrieben:
58 = 0011 1010 in binary. Hex uses four bits (nibble). You have to look at the upper four bits (3), then the lower four bits (A).
A WORD uses two bytes (four nibbles) and can have a number from 0000 to FFFF hex.
A BYTE uses one byte (two nibbles) and can have a number from 00 to FF hex.
Here is a crude function to convert a BYTE to a two char string. It is up to you to expand it for longer variable types...
FUNCTION BYTE_TO_HEX_STRING : STRING
VAR_INPUT
Input : BYTE;
END_VAR
VAR
Temp : BYTE;
Chr : BYTE;
Ans : STRING;
END_VAR
Temp := SHR(Input, 4);
IF Temp > 9 THEN
Chr := Temp + 55;
ELSE
Chr := Temp + 48;
END_IF
Ans[0] := Chr;
Temp := SHR(SHL(Input,4),4); //wipes upper nibble (could also mask...)
IF Temp > 9 THEN
Chr := Temp + 55;
ELSE
Chr := Temp + 48;
END_IF
Ans[1] := Chr;
Ans[2] := 0; //null char
BYTE_TO_HEX_STRING := Ans;
Thanks
I managed to get it to work for longer variable types. Worked like a charm
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Hello NBCC, I work on the same problem as you but I have to work with WORDs and not BYTEs, could you please explain me how did you managed to get it to work for longer variable types ?
I know this is an old post but I still hope that someone will answer...
in fact (this question is more for scott_cunningham) , I don't understand why and what is really done here :
"
Temp := SHR(Input, 4);
IF Temp > 9 THEN
Chr := Temp + 55;
ELSE
Chr := Temp + 48;
END_IF
"
Thanks
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Hi all,
I have converted an array of bytes into a string. Worked like a charm with the oscat function btw.
Now i have to put it into hex-values. Any way to create a string with hex values?
Thanks
Originally created by: scott_cunningham
You can make your own solution by using two offsets. Look at each byte. If it is >=10 then add 55 to it (A is ascii 65). For smaller number, add ascii offset for 0 - sorry don't remember if it is 40 or 42). Examine bytes from highest to lowest and con at your chars.
There is probably a prebuilt function somewhere, but it may take you longer searching for it than building it.
OK that's great but still facing a major problem for me.
Let's say i got value 58 in one byte. Then i want it displayed as 3A. (Hex is 4 bits). And i don't see any solution in it then
Originally created by: scott_cunningham
58 = 0011 1010 in binary. Hex uses four bits (nibble). You have to look at the upper four bits (3), then the lower four bits (A).
A WORD uses two bytes (four nibbles) and can have a number from 0000 to FFFF hex.
A BYTE uses one byte (two nibbles) and can have a number from 00 to FF hex.
Here is a crude function to convert a BYTE to a two char string. It is up to you to expand it for longer variable types...
FUNCTION BYTE_TO_HEX_STRING : STRING
VAR_INPUT
Input : BYTE;
END_VAR
VAR
Temp : BYTE;
Chr : BYTE;
Ans : STRING;
END_VAR
Temp := SHR(Input, 4);
IF Temp > 9 THEN
Chr := Temp + 55;
ELSE
Chr := Temp + 48;
END_IF
Ans[0] := Chr;
Temp := SHR(SHL(Input,4),4); //wipes upper nibble (could also mask...)
IF Temp > 9 THEN
Chr := Temp + 55;
ELSE
Chr := Temp + 48;
END_IF
Ans[1] := Chr;
Ans[2] := 0; //null char
BYTE_TO_HEX_STRING := Ans;
Thanks
I managed to get it to work for longer variable types. Worked like a charm
Hello NBCC, I work on the same problem as you but I have to work with WORDs and not BYTEs, could you please explain me how did you managed to get it to work for longer variable types ?
I know this is an old post but I still hope that someone will answer...
in fact (this question is more for scott_cunningham) , I don't understand why and what is really done here :
"
Temp := SHR(Input, 4);
IF Temp > 9 THEN
Chr := Temp + 55;
ELSE
Chr := Temp + 48;
END_IF
"
Thanks
Hi kdkwhite,
for Word you still can use suggested code by using a union structure and crack down your Word to two byte as bellow:
then define your variable as this type:
now assign your Word variable input to InWord and send OutBytes[x] to the mentioned method:
Regarding your question about the code: actually 48 is ascii code of "0" and while 65 is the ascii code of "A" so in above code 55 + 10 would be 65.
Last edit: alimans 2023-09-20