ganeshredcobra ganeshredcobra - 2 months ago 13
C Question

Converting ascii hex string to byte array

I have a char array say

char value []={'0','2','0','c','0','3'};


I want to convert this into a byte array like
unsigned char val[]={'02','0c','03'}


This is in an embedded application so i can't use string.h functions. How can i do this?

Answer

Sicne you talk about an embedded application I assume that you want to save the numbers as values and not as strings/characters. So if you just want to store your character data as numbers (for example in an integer), you can use sscanf.

This means you could do something like this:

 char source_val[] = {'0','A','0','3','B','7'} // Represents the numbers 0x0A, 0x03 and 0xB7
 uint8 dest_val[3];                            // We want to save 3 numbers
 for(int i = 0; i<3; i++)
 {
     sscanf(&source_val[i*2],"%x%x",&dest_val[i]); // Everytime we read two chars --> %x%x
 }
 // Now dest_val contains 0x0A, 0x03 and 0xB7

However if you want to store it as a string (like in your example), you can't use unsigned char since this type is also just 8-Bit long, which means it can only store one character. Displaying 'B3' in a single (unsigned) char does not work.

edit: Ok according to comments, the goal is to save the passed data as a numerical value. Unfortunately the compiler from the opener does not support sscanf which would be the easiest way to do so. Anyhow, since this is (in my opinion) the simplest approach, I will leave this part of the answer at it is and try to add a more custom approach in this edit.

Regarding the data type, it actually doesn't matter if you have uint8. Even though I would advise to use some kind of integer data type, you can also store your data into an unsigned char. The problem here is, that the data you get passed, is a character/letter, that you want to interpret as a numerical value. However, the internal storage of your character differs. You can check the ASCII Table, where you can check the internal values for every character. For example:

char letter = 'A'; // Internally 0x41 
char number = 0x61; // Internally 0x64 - represents the letter 'a'

As you can see there is also a differnce between upper an lower case.

If you do something like this:

int myVal = letter;  //

myVal won't represent the value 0xA (decimal 10), it will have the value 0x41.

The fact you can't use sscanf means you need a custom function. So first of all we need a way to conver one letter into an integer:

int charToInt(char letter)
{
    int myNumerical;
    // First we want to check if its 0-9, A-F, or a-f) --> See ASCII Table
    if(letter > 47 && letter < 58)
    {
        // 0-9
        myNumerical = letter-48;
        // The Letter "0" is in the ASCII table at position 48 -> meaning if we subtract 48 we get 0 and so on...
    }
    else if(letter > 64 && letter < 71)
    {
       // A-F
       myNumerical = letter-55 
       // The Letter "A" (dec 10) is at Pos 65 --> 65-55 = 10 and so on..
    }
    else if(letter > 96 && letter < 103)
    {
       // a-f
       myNumerical = letter-87
       // The Letter "a" (dec 10) is at Pos 97--> 97-87 = 10 and so on...
    }
    else
    {
       // Not supported letter...
       myNumerical = -1;
    }
    return myNumerical;
}

Now we have a way to convert every single character into a number. The other problem, is to always append two characters together, but this is rather easy:

int appendNumbers(int higherNibble, int lowerNibble)
{
     int myNumber = higherNibble << 4;
     myNumber |= lowerNibbler;
     return myNumber;
    // Example: higherNibble = 0x0A, lowerNibble = 0x03;  -> myNumber 0 0xA3
    // Of course you have to ensure that the parameters are not bigger than 0x0F 
}

Now everything together would be something like this:

 char source_val[] = {'0','A','0','3','B','7'} // Represents the numbers 0x0A, 0x03 and 0xB7
 int dest_val[3];                             // We want to save 3 numbers
 int temp_low, temp_high;
 for(int i = 0; i<3; i++)
 {
     temp_high = charToInt(source_val[i*2]);
     temp_low = charToInt(source_val[i*2+1]);
     dest_val[i] = appendNumbers(temp_high , temp_low);
 }

I hope that I understood your problem right, and this helps..