Hi, I am trying to figure out how to convert data which is in ASCII (40863284) to hexadecimal (0x40,0x86,0x32,0x84).
For example, I am sending a command to the Arduino Ethernet shield (/?xbee=40863284) and I want my program to send API packets in this format (0x40,0x86,0x32,0x84).
I am doing this for my home management system and I want to trigger relays remotely from different xbees.
I have searched the forums but all i found are Ascii to Decimal conversions and other codes that doesn't seem to fit my criteria.
I suppose that when you write that the data are in ascii it means you get it in a string value. so from you must divide it and then convert it to his hex value. Is that it ?
so from :
char toExtract[]="40863284";
int convertedValues[4];
for (int i=0; i<8; i+=2)
{
convertedValues[i/2] = (toExtract[i]-'0')*16 + (toExtract[i+1]-'0');
}
Now you get 4 values that correspond to the hexadecimal value. There is a lot of way about doing that, this is an version.
Exemple for '32' :
convert '3' to int : '3'-'0' gives the number 3, then by 16 gives 48. We add '2'-'0' that gives the number 2, which added to 48 gives 50.