Page 1 of 1

Convert text (Ascci) to Hex

Posted: Sat Aug 17, 2019 10:33 am
by jollybv
Hi Guys

I'm trying to convert a Text message to Hex so I can send an SMS in PDU Mode, I have searched and searched but can't seem to find a formula or an example with no luck. I know that with 1 to 9 you would add 30 but from there I'm really lost.

Re: Convert text (Ascci) to Hex

Posted: Sat Aug 17, 2019 1:12 pm
by mnf
It's not too bad to do:

Assuming you want to convert everything to a hex string?

For example:
'A' = 65 =0x41 (or 41?)

So if .c = a character then add to the sms (0x if using) and HexDigit(.c >>4) and HexDigit(.c & 0x0f)

Where HexDigit adds '0' to value if it is 0..9 and 'A' - 10 to value otherwise...
(in our examples case 'A' >> 4 (/16) = 4 (which is <=9 so add '0' -> '4')
and 'A' & 0xf = 1)

If you just want to convert numbers then things are a little different...

I can post a FC sample later - if needed?

Martin

Re: Convert text (Ascci) to Hex

Posted: Sun Aug 18, 2019 6:01 am
by jollybv
Hi Martin

Thanks for the reply I would really appreciate the sample code so I can see how you implement this. I will need to convert most of the Ascii table as I'm sending a long SMS in pdf format :?

Re: Convert text (Ascci) to Hex

Posted: Sun Aug 18, 2019 10:12 am
by mnf
- sending the message as a hex string seems strange?

But - it is easy to do -
StrToHex.fcfx
(17.73 KiB) Downloaded 284 times
Has 3 demonstrations
1) convert a string to hex a character at a time (output to UART)
2) Convert a string to HEX (so 'ABC' -> '414243') again output to UART. Note that here the output string MUST be large enough to hold all the hex string and a 0C terminator. So this technique might not be suitable on some MCUs with limited RAM?
3) Convert ints 0..65535 to 4 digit strings and output to UART.

No 2 -is most likely what you need?

note that FC has a NumberToHex$ function that converts a number to '0xABCD' format - which you can also use - I'd assumed you didn't really want the 0x at the front of each number so it is probably easier & quicker to convert using our own macro than using the FC one then stripping off the 'Ox'

Martin

Re: Convert text (Ascci) to Hex

Posted: Mon Aug 19, 2019 7:01 am
by jollybv
Hi Martin

Thanks for this will have a look and let you know how i get on. :D