title: How to correctly convert a Hex String to Byte Array in C?
body: <p>I need to convert a string, containing hex values as characters, into a byte array. Although this has been answered <a href="https://stackoverflow.com/questions/3408706/hex-string-to-byte-array-in-c">already here</a> as the first answer, I get the following error:</p>
  
  <pre><code>warning: ISO C90 does not support the ‘hh’ gnu_scanf length modifier [-Wformat]
  </code></pre>
  
  <p>Since I do not like warnings, and the omission of <code>hh</code> just creates another warning</p>
  
  <pre><code>warning: format ‘%x’ expects argument of type ‘unsigned int *’, but argument 3 has type ‘unsigned char *’ [-Wformat]
  </code></pre>
  
  <p>my question is: How to do this right? For completion, I post the example code here again:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  
  int main(int argc, char **argv)
  {
      const char hexstring[] = "deadbeef10203040b00b1e50", *pos = hexstring;
      unsigned char val[12];
      size_t count = 0;
  
       /* WARNING: no sanitization or error-checking whatsoever */
      for(count = 0; count &lt; sizeof(val)/sizeof(val[0]); count++) {
          sscanf(pos, "%2hhx", &amp;val[count]);
          pos += 2 * sizeof(char);
      }
  
      printf("0x");
      for(count = 0; count &lt; sizeof(val)/sizeof(val[0]); count++)
          printf("%02x", val[count]);
      printf("\n");
  
      return(0);
  }
  </code></pre>