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.
Convert text (Ascci) to Hex
Moderator: Benj
-
- Valued Contributor
- Posts: 1189
- Joined: Wed May 31, 2017 11:57 am
- Has thanked: 70 times
- Been thanked: 439 times
- Contact:
Re: Convert text (Ascci) to Hex
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
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
- jollybv
- Flowcode v5 User
- Posts: 374
- Joined: Thu Feb 12, 2009 5:20 am
- Location: Cape Town
- Has thanked: 81 times
- Been thanked: 25 times
- Contact:
Re: Convert text (Ascci) to Hex
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
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
-
- Valued Contributor
- Posts: 1189
- Joined: Wed May 31, 2017 11:57 am
- Has thanked: 70 times
- Been thanked: 439 times
- Contact:
Re: Convert text (Ascci) to Hex
- sending the message as a hex string seems strange?
But - it is easy to do - 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
But - it is easy to do - 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
- jollybv
- Flowcode v5 User
- Posts: 374
- Joined: Thu Feb 12, 2009 5:20 am
- Location: Cape Town
- Has thanked: 81 times
- Been thanked: 25 times
- Contact:
Re: Convert text (Ascci) to Hex
Hi Martin
Thanks for this will have a look and let you know how i get on.
Thanks for this will have a look and let you know how i get on.