title: Write hexadecimal to file in C (not ASCII)
body: <p>I am trying to convert some binary strings to hexadecimal values and write it directly into the file. I mean the values should be visible only through hex editor and I do not care about ASCII text in the file.</p>
  
  <p>Here is part of my code:</p>
  
  <pre><code>unsigned int value;
  char p[4];
  FILE *fp;
  fp = fopen("file.txt","wb+");
  
  //sprintf(p, "\\x%x", hex);
  
  value = strtol("10010011", NULL, 2);
  fwrite(&amp;value, strlen(p), 1, fp);
  
  //fwrite(p, strlen(p), 1, fp);
  </code></pre>
  
  <p>I tried to use fwrite to write the integer to file. It seems to work, but it will write 4 bytes hex (padding with 00s) instead of 1 byte. I also tried to print a string starting with "\x" and write the string to file (comment out this piece of code), but it will write in ASCII format including "\x" to file that is not what I want. Does anyone have an idea about how to do this or what's wrong with my code? Thanks. </p>