I have a string of 40 characters: "7E001017010013A2004063D9BBFFFE0244310483".
I need to convert these characters into a HEX string of 20 characters:
"0x7E, 0x00, 0x10, 0x17, 0x01, 0x00, 0x13, 0xA2, 0x00, 0x40, 0x63, 0xD9, 0xBB, 0xFF, 0xFE, 0x02, 0x44, 0x31, 0x04, 0x83"
Is there a function that will allow me to achieve this.
sprintf() maybe...
You can access each character within a string with the [] operator. You can format a string with sprintf:
char *in = "7E001017010013A2004063D9BBFFFE0244310483";
char out[101];
sprintf(out,"0x%c%c 0x%c%c 0x%c%c 0x%c%c 0x%c%c 0x%c%c 0x%c%c 0x%c%c 0x%c%c 0x%c%c 0x%c%c 0x%c%c 0x%c%c 0x%c%c 0x%c%c 0x%c%c 0x%c%c 0x%c%c 0x%c%c 0x%c%c 0x%c%c 0x%c%c 0x%c%c 0x%c%c 0x%c%c 0x%c%c 0x%c%c 0x%c%c 0x%c%c 0x%c%c 0x%c%c 0x%c%c 0x%c%c 0x%c%c 0x%c%c 0x%c%c 0x%c%c 0x%c%c 0x%c%c 0x%c%c",
in[0],in[1],in[2],in[3],in[4],in[5],in[6],in[7],in[8],in[9],in[10],in[11],in[12],in[13],in[14],in[15],in[16],in[17],in[18],in[19],
in[20],in[21],in[22],in[23],in[24],in[25],in[26],in[27],in[28],in[29],in[30],in[31],in[32],in[33],in[34],in[35],in[36],in[37],in[38],in[39]);
If the incoming string is in a String object, then you can use the .charAt() method instead of the [] operator to get the individual characters. The formatting is the same.
printf(out,"0x%c%d,
Why the "%d"?
Why not "%c%c" ?
This function will take a single ASCII character in and return a value corresponding to the ASCII hex interpretation of the input:-
byte convertFromHex(char ascii){
if(ascii > 0x39) ascii -= 7; // adjust for hex letters upper or lower case
return(ascii & 0xf);
}
Then all you need to do is to feed it with your input one char at a time and catch the output in where ever you want to accumulate it.
It does no error checking so that if you feed it a character that is not an ASCII hex value it will still return a number.
AWOL:
printf(out,"0x%c%d,
Why the "%d"?
Why not "%c%c" ?
Typo. It should be %c%c. You may notice it's regular - you can see where I copied and pasted the error in blocks of 5
robcole:
I have a string of 40 characters: "7E001017010013A2004063D9BBFFFE0244310483".
I need to convert these characters into a HEX string of 20 characters:
"0x7E, 0x00, 0x10, 0x17, 0x01, 0x00, 0x13, 0xA2, 0x00, 0x40, 0x63, 0xD9, 0xBB, 0xFF, 0xFE, 0x02, 0x44, 0x31, 0x04, 0x83"
Is there a function that will allow me to achieve this.
What you're asking for is NOT ascii to hex. You've got a sequence of hex characters in a string and you're trying to insert a few characters in between each pair of hex characters. That's easily done.
You could use majenko's approach of processing all the hex character pairs in a huge printf statement, but it would be more general to provide something that handled an arbitrary length string by processing it one hex character pair at a time - it would reduce the scope for copy/paste errors too.
PeterH, yes I agree, but how would I go about processing it one hex character pair at a time?
for(int i=0; i<strlen(in); i+=2)
{
// Do something with in[i] and in[i+1];
}
majenko,
Many thanks. But with my very limited programming knowledge, I have no idea as to
Do something with in[i] and in[i+1];
Moderator edit: Inadvertent italics removed.
I have no idea as to
Do something with in and in[i+1];
Well no nether have we because you have not said what you want to do!
That code extracts digits two at at time, like you ask, so what do you want to do?
I have a string of 40 characters: "7E001017010013A2004063D9BBFFFE0244310483" that is received on the Serial port.
I need to create a HEX string of 20 bytes to send to an XBee device. The HEX string will be:
7E 00 10 17 01 00 13 A2 00 40 63 D9 BB FF FE 02 44 31 04 83
That is what I am attempting to achieve
So, your "something" is what Mike wrote in reply #4.
Convert in [i] to a hex digit and multiply it by 16, then convert in [i + 1] to a hex digit, and add it the two.
Voila, you've reconstructed your hex values.
The HEX string will be:
7E 00 10 17 01 00 13 A2 00 40 63 D9 BB FF FE 02 44 31 04 83
That's a different format to what you said before.
I am confused by what you actually need.
Do you want to end up with an array of 20 bytes which are made up from pairs of letter/numbers from a string of 40 characters?
- or -
Do you want a string of characters made up from the incoming string with the letters / numbers shuffled around and spaced out?