Page 1 of 1

Byte or INT number into a ASCII hex string

Posted: Tue Aug 27, 2024 10:42 pm
by dvcam99
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

Re: Byte or INT number into a ASCII hex string

Posted: Wed Aug 28, 2024 8:30 am
by chipfryer27
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

Re: Byte or INT number into a ASCII hex string

Posted: Wed Aug 28, 2024 4:31 pm
by S_VE
Hi Dirk

do you want to convert INT or Byte to STRING ( String is an ARRAY of BYTES)?

Just check this..
FC INT to String.jpg
FC INT to String.jpg (95.85 KiB) Viewed 3821 times

Re: Byte or INT number into a ASCII hex string

Posted: Thu Aug 29, 2024 7:06 am
by dvcam99
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.

Screenshot 2024-08-29 080847.png
Screenshot 2024-08-29 080847.png (102.85 KiB) Viewed 3790 times
BR
Dirk

Re: Byte or INT number into a ASCII hex string

Posted: Thu Aug 29, 2024 8:13 am
by mnfisher
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

Re: Byte or INT number into a ASCII hex string

Posted: Thu Aug 29, 2024 10:31 am
by mnfisher
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