Byte or INT number into a ASCII hex string

For general Flowcode discussion that does not belong in the other sections.
Post Reply
dvcam99
Posts: 96
http://meble-kuchenne.info.pl
Joined: Fri Dec 04, 2020 11:03 am
Has thanked: 6 times
Been thanked: 12 times

Byte or INT number into a ASCII hex string

Post 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
Happy FC9, FC-8 and FC-6 professional user ;)

chipfryer27
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

Post 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

S_VE
Posts: 26
Joined: Tue Jan 26, 2021 6:10 am
Has thanked: 32 times
Been thanked: 1 time

Re: Byte or INT number into a ASCII hex string

Post 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 3701 times
S_V

dvcam99
Posts: 96
Joined: Fri Dec 04, 2020 11:03 am
Has thanked: 6 times
Been thanked: 12 times

Flowcode v9 Re: Byte or INT number into a ASCII hex string

Post 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 3670 times
BR
Dirk
Happy FC9, FC-8 and FC-6 professional user ;)

mnfisher
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

Post 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

mnfisher
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

Post 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

Post Reply