Hello FC Community,
I belive I need some help. I want to convert a Byte or INT number into a ASCII hex string.
Example: DEC123 = 0x7B should look like in ASCII HEX string 313233
I tried to use HEX2string or 2string, unfortunately these function are nor give the required result.
BR
Dirk
Byte or INT number into a ASCII hex string
-
- Posts: 96
- http://meble-kuchenne.info.pl
- Joined: Fri Dec 04, 2020 11:03 am
- Has thanked: 6 times
- Been thanked: 12 times
-
- Valued Contributor
- Posts: 1528
- Joined: Thu Dec 03, 2020 10:57 am
- Has thanked: 353 times
- Been thanked: 549 times
Re: Byte or INT number into a ASCII hex string
Hi
What is it you actually want to do?
For example if you wanted to send 123 over the UART then SendNumber would convert and send them as ASCII.
Regards
What is it you actually want to do?
For example if you wanted to send 123 over the UART then SendNumber would convert and send them as ASCII.
Regards
Re: Byte or INT number into a ASCII hex string
Hello together,
many thanks for your suggestions. I will check it out.
In between I found a way to solve my problem. I used the string function "ToString$" too. After the function I used switch component in order to filter for string element 0-2. 0 is giving me the hundert, 1 is giving me the ten, and 2 is giving me the numbers smaller then 10. Each switch has ten ways.
Each switch way down is giving me the requried ASCII HEX.
BR
Dirk
many thanks for your suggestions. I will check it out.
In between I found a way to solve my problem. I used the string function "ToString$" too. After the function I used switch component in order to filter for string element 0-2. 0 is giving me the hundert, 1 is giving me the ten, and 2 is giving me the numbers smaller then 10. Each switch has ten ways.
Each switch way down is giving me the requried ASCII HEX.
BR
Dirk
Happy FC9, FC-8 and FC-6 professional user 

-
- Valued Contributor
- Posts: 1453
- Joined: Wed Dec 09, 2020 9:37 pm
- Has thanked: 135 times
- Been thanked: 707 times
Re: Byte or INT number into a ASCII hex string
Try using a macro to convert a digit to a hex character.
Sorry - on a phone now but:
HexDigit(n) // Convert a number (0..15) to a hex character (return a char)
If .n >= 10
result = .n - 10 + 'A' // 10 = 'A', 11 = 'B' etc
else
result = .n + '0'
Martin
Sorry - on a phone now but:
HexDigit(n) // Convert a number (0..15) to a hex character (return a char)
If .n >= 10
result = .n - 10 + 'A' // 10 = 'A', 11 = 'B' etc
else
result = .n + '0'
Martin
-
- Valued Contributor
- Posts: 1453
- Joined: Wed Dec 09, 2020 9:37 pm
- Has thanked: 135 times
- Been thanked: 707 times
Re: Byte or INT number into a ASCII hex string
Do a complete number (.n)
.pos = 0
repeat
.str[.pos] = HexDigit(.n % 16)
.n = .n / 16
.pos = .pos + 1
until .n = 0
If you want the 0x set .str = "0x" and .pos to 2 beforehand...
Again - I would pull this to a separate macro and return a string. It will work for any 'size' of n (so byte, word or long)
Martin
.pos = 0
repeat
.str[.pos] = HexDigit(.n % 16)
.n = .n / 16
.pos = .pos + 1
until .n = 0
If you want the 0x set .str = "0x" and .pos to 2 beforehand...
Again - I would pull this to a separate macro and return a string. It will work for any 'size' of n (so byte, word or long)
Martin