Convert ASCII to HEX

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.

and we’ve the good value because 0x32 = 50 !

Grag38 … Ummm… What happens with A-F with that code?

This function will convert a character representation of a hexidecimal digit into the actual hexidecimal value:

unsigned char h2d(unsigned char hex)
{
        if(hex > 0x39) hex -= 7; // adjust for hex letters upper or lower case
        return(hex & 0xf);
}

Use that to create your pairs of numbers, and use a 4-bit left shift to combine them:

unsigned char high_nibble,low_nibble,value;

high_hibble = h2d(incoming[0]);
low_nibble = h2d(incoming[1]);

value = (high_nibble << 4) | low_nibble;

Do that for each pair in a loop, and you’re good to go.

Grag38 ... Ummm... What happens with A-F with that code?

OUPSSSS !!!

Yes that's what I am trying to achieve, thanks for providing the assistance.

I will give it a shot and see then.

i had below Hex value from serial Rx pin.

006900730020006D0065007300730061006700650020007700610073002000730065006E007400200074006F00200079006F0075002000620079002000310030002D0053007400720069006B00650020004E006500740077006F0072006B0020004D006F006E00690074006F007200200066006F007200200074006800650020

How store it in to String variable?

amalanjula:
i had below Hex value from serial Rx pin.

006900730020006D0065007300730061006700650020007700610073002000730065006E007400200074006F00200079006F0075002000620079002000310030002D0053007400720069006B00650020004E006500740077006F0072006B0020004D006F006E00690074006F007200200066006F007200200074006800650020

How store it in to String variable?

Please start a new thread instead of reviving a three-plus year old one.

1 Like

Going back to original post, what about:

void setup() {

  Serial.begin(9600);

  byte toExtract[] = "40863284";
  byte convertedValues[4];
  
  boolean flag = atoh(convertedValues, toExtract);

  if (flag != NULL)
  {
    for (int i = 0; i < sizeof(convertedValues); i++) 
    {
      Serial.print(convertedValues[i], HEX);
      Serial.print("   in decimal: ");
      Serial.println(convertedValues[i]);
    }
  }
}

void loop() {

}

byte *atoh(byte *destination, const byte *source)
{   
    byte *ret = destination;

    for(int lsb, msb; *source; source += 2)
    {   
        msb = tolower(*source);
        lsb = tolower(*(source + 1));
        msb -= isdigit(msb) ? 0x30 : 0x57;
        lsb -= isdigit(lsb) ? 0x30 : 0x57;
        if((msb < 0x0 || msb > 0xf) || (lsb < 0x0 || lsb > 0xf))
        {
            *ret = 0;
            return NULL;
        }
        *destination++ = (char)(lsb | (msb << 4));  
    }
    *destination = 0;
    return ret;
}