0

I want to convert this char array of formatted hex values to a lookup table of hex values.
Something like this:

char crc_input[300] = "abcd12344f..."; // input
unsigned char buf[4096] = { 0xab, 0xcd, 0x12, ... }; // output

I tried this code:

char crc_input[300] = "abcd12344f";
unsigned char buffer[20];
unsigned char output[400];

buffer[0] = crc_input[0]; // a
buffer[1] = crc_input[1]; // b

sprintf(output[0], "0x%s%s", buffer[0], buffer[1]);

printf("output[0] : %s",output[0]);

But apparently the unsigned char array can't hold entire 0xXX value. Is there any other way to approach this?

Jorengarenar
  • 2,705
  • 5
  • 23
  • 60
Othmane
  • 39
  • 4
  • 1
    Does this answer your question? [How to correctly convert a Hex String to Byte Array in C?](https://stackoverflow.com/questions/18267803/how-to-correctly-convert-a-hex-string-to-byte-array-in-c) – Gerhardh Dec 03 '21 at 13:53

1 Answers1

0

There is a basic flaw in your approach, which is sprintf is not likely what you want. It will not do conversion of data, only interpretations of data. Additionally, that function wants a pointer to a char array, but you are passing in the first element of the array instead.

It appears you want to do conversion of ASCII HEX encoded string into an array of bytes, so the suggestion offered by @Gerhardh is spot on. If you really want to code your own routine, then I'd suggest you look at the sscanf function instead.

icodeplenty
  • 252
  • 1
  • 6