title: How to convert string to int32 to hex and store into buffer
body: <p>I'm trying to convert a string to string and then to hex. The string value represent for example an integer like 266. The main reason why I do this, I store all my values in the string and want send them over network to a host, therefore I must represent all values as hex. If I convert string "<code>266</code>" to int32 and then to hex "<code>0x010a</code>" and store the hex value in a buffer, the buffer contains "<code>30 31</code>" and not 0x010a. Here is an running example of my problem.</p>
  
  <pre><code>#include &lt;stdio.h&gt; /* printf */
  #include &lt;stdint.h&gt; /* int32_t*/
  #include &lt;stdlib.h&gt; /* strtol, malloc */
  #include &lt;string.h&gt; /* memcpy */
  
  int integer_to_hex(
          char **out_hex,
          int *out_len,
          int32_t value)
  {
      *out_len = 0;
      *out_hex = NULL;
      if ( value &lt; 128 &amp;&amp; value &gt; -128)
      {
          *out_len = 1;
          *out_hex = malloc(*out_len);
          sprintf(*out_hex,"%02x", value &amp; 0xff);
          return 0;
      }
      else if (value &lt; 32767 &amp;&amp; value &gt; -32767)
      {
          *out_len = 2;
          *out_hex = malloc(*out_len);
          sprintf(*out_hex,"%04x", value);
          return 0;
      }
      return -1;
  }
  
  int main()
  {
      char *value_str = "266";
      int32_t val_int = strtol(value_str,NULL,10);
      char *out_hex;
      int out_len;
      int ret_val = integer_to_hex(&amp;out_hex, &amp;out_len, val_int);
      printf("ret_val=%d\n", ret_val);
      printf("out=%s\n", out_hex);
      printf("out_len=%d\n", out_len);
      char *buffer = malloc(out_len);
      memcpy(&amp;buffer, &amp;out_hex, out_len);
      int i;
      for(i=0;i&lt;out_len;i++)
          printf("buffer=%02x\n", buffer[i]);
      return 0;
  }
  </code></pre>
  
  <p>The output:</p>
  
  <pre><code>ret_val=0
  out=010a
  out_len=2
  buffer=30
  buffer=31
  </code></pre>