How to pack a hexadecimal value in an unsigned char variable in a C program?

I have a hexadecimal value "F69CF355B6231FDBD91EB1E22B61EA1F" in a string and I am using this value in my program by hardcoding the value in an unsigned char variable like this: unsigned char a[] = { 0xF6 ,0x9C ,0xF3 ,0x55 ,0xB6 ,0x23 ,0x1F ,0xDB ,0xD9 ,0x1E ,0xB1 ,0xE2 ,0x2B ,0x61 ,0xEA ,0x1F}; Is there any function or any other method by which I can take the value from a string and put it into an unsigned variable in the hexadecimal format by packing it?


#include <stdio.h> #include <ctype.h> int hctoi(const char h){ if(isdigit(h)) return h - '0'; else return toupper(h) - 'A' + 10; } int main(void){ const char cdata[]="F69CF355B6231FDBD91EB1E22B61EA1F"; unsigned char udata[(sizeof(cdata)-1)/2]; const char *p; unsigned char *up; for(p=cdata,up=udata;*p;p+=2,++up){ *up = hctoi(p[0])*16 + hctoi(p[1]); } { //check code int i; for(i=0;i<sizeof(udata);++i) printf("%02X", udata[i]); } return 0; }


You can translate a hexadecimal value in a string into a value using sscanf. If you want an array of values, then you could write a function to split up the input string into two character segments and use sscanf to convert each piece. (I haven't done C for an eternity so I don't know if that's a good way to do it.)


If it's for 32 single hex-values (16 bytes, 128bit) only then you might take look at the methods provided by libuuid.

libuuid is part of the e2fsprogs package. Anyhow some linux distros, Debian for example, ship libuuid as a separate package. To use the Debian package for your developement you also need to look here.


Check this answer for doing this stuff in c++ using sscanf().

For c, it would be something like this:

char *str = "F69CF355B6231FDBD91EB1E22B61EA1F"; char substr[3] = "__"; unsigned char *a = NULL; len = strlen(str); a = malloc(sizeof(unsigned char)*(len/2)+1); for ( i = 0; i < len/2; i++) { substr[0] = str[i*2]; substr[1] = str[i*2 + 1]; sscanf( substr, "%hx", &a[i] ); } free(a);


Introduce auxiliary functions data_length and data_get to easily iterate over your data. The following program dumps unpacked unsigned chars on stdout, one per line:

#include <stdlib.h> #include <stdio.h> #include <ctype.h> #include <string.h> /* htoi(H) Return the value associated to the hexadecimal digit H. */ int htoi(char h) { int a = -1; if(isdigit(h)) { a = h - '0'; } else { a = toupper(h) - 'A' + 10; } return a; } /* data_length(D) The length of the data stored at D. */ int data_length(const char* d) { return strlen(d) / 2; } /* data_get(D, K) Return the K-th unsigned char located encoded in d. */ unsigned char data_get(const char *d, int k) { return htoi(d[2*k]) * 0x10 + htoi((d+1)[2*k]); } int main() { const char cdata[]="F69CF355B6231FDBD91EB1E22B61EA1F"; for(int i = 0; i < data_length(cdata); ++i) { printf("0x%02hhx\n", data_get(cdata, i)); } return EXIT_SUCCESS; }


  • Split numeric part from alphanumeric string using C#
  • How to split string at operators
  • iTextSharp - very large table memory leak
  • Windows Phone ANID to ANID2 conversion on C#?
  • Datagrid and column count
  • How can I distinguish between integer and character when using scanf()
  • Split exponential number string representation into power and exponent
  • How do I make letters to uppercase after each of a set of specific characters
  • JavaScript In KML Ignored By Google Earth Plugin
  • Tag and Element shortcuts - Sublime Text 2
  • Call C++ class member function from C (Visual Studio)
  • Reaping zombie process - child
  • how to display   in Mozilla using XSL.
  • Detecting null parameter in preprocessor macro
  • output of program is not same as passed argument
  • Websockets service method fails during R startup
  • Alternatives to the OPTIONAL fallback SPARQL pattern?
  • How to get next/previous record number?
  • Apache 2.4 - remove | delete | uninstall
  • Akka Routing: Reply's send to router ends up as dead letters
  • Proper way to use connect-multiparty with express.js?
  • Trying to get generic when generic is not available
  • embed rChart in Markdown
  • How to get Windows thread pool to call class member function?
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • unknown Exception android
  • EntityFramework adding new object to nested object collection
  • Checking variable from a different class in C#
  • Django query for large number of relationships
  • costura.fody for a dll that references another dll
  • Why is Django giving me: 'first_name' is an invalid keyword argument for this function?
  • Observable and ngFor in Angular 2
  • How to Embed XSL into XML
  • How can I use `wmic` in a Windows PE script?
  • failed to connect to specific WiFi in android programmatically
  • UserPrincipal.Current returns apppool on IIS
  • Conditional In-Line CSS for IE and Others?
  • java string with new operator and a literal
  • How to push additional view controllers onto NavigationController but keep the TabBar?
  • How can I use threading to 'tick' a timer to be accessed by other threads?