title: Convert ascii char[] to hexadecimal char[] in C
body: <p>I am trying to convert a char[] in ASCII to char[] in hexadecimal.</p>
  
  <p>Something like this:</p>
  
  <p>hello --> 68656C6C6F</p>
  
  <p>I want to read by keyboard the string. It has to be 16 characters long.</p>
  
  <p>This is my code now. I don't know how to do that operation. I read about strol but I think it just convert str number to int hex...</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  main()
  {
      int i = 0;
      char word[17];
  
      printf("Intro word:");
  
      fgets(word, 16, stdin);
      word[16] = '\0';
      for(i = 0; i&lt;16; i++){
          printf("%c",word[i]);
      }
   }
  </code></pre>
  
  <p>I am using fgets because I read is better than fgets but I can change it if necessary.</p>
  
  <p>Related to this, I am trying to convert the string read in a uint8_t array, joining each 2 bytes in one to get the hex number.</p>
  
  <p>I have this function which I am using a lot in arduino so I think it should work in a normal C program without problems.</p>
  
  <pre><code>uint8_t* hex_decode(char *in, size_t len, uint8_t *out)
  {
      unsigned int i, t, hn, ln;
  
      for (t = 0,i = 0; i &lt; len; i+=2,++t) {
  
              hn = in[i] &gt; '9' ? (in[i]|32) - 'a' + 10 : in[i] - '0';
              ln = in[i+1] &gt; '9' ? (in[i+1]|32) - 'a' + 10 : in[i+1] - '0';
  
              out[t] = (hn &lt;&lt; 4 ) | ln;
              printf("%s",out[t]);
      }
      return out;
  </code></pre>
  
  <p>}</p>
  
  <p>But, whenever I call that function in my code, I get a segmentation fault.</p>
  
  <p>Adding this code to the code of the first answer:</p>
  
  <pre><code>    uint8_t* out;
      hex_decode(key_DM, sizeof(out_key), out);
  </code></pre>
  
  <p>I tried to pass all necessary parameters and get in out array what I need but it fails...</p>