convert hex string to uchar[n]

Suppose that I have a number in hexadecimal notation
with size 2n, like "aa00F234AA". I want to write it in
an unsigned char array of size n ("aa" in the first
byte, "00" in the second byte, etc) . In fact I want a
function like this
 
void f(unsigned char* dest, char* str, int sizeOfDest);

How can I do this?
Thanks in advance.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
int chhex(char ch)
{
    if(isdigit(ch))
        return ch - '0';
    if(tolower(ch) >= 'a' && tolower(ch) <= 'f')
        return ch - 'a' + 10;
    return -1;
}

void f(unsigned char *dest, const char *source, int bytes_n)
{
    for(bytes_n--; bytes_n >= 0; bytes_n--)
        dest[bytes_n] = 16 * chhex(source[bytes_n*2]) + chhex(source[bytes_n*2 + 1]);
}
Topic archived. No new replies allowed.