title: writing to a binary file in c++
body: <p>My problem is that I have a string in hex representation say:
  <code>036e</code>. I want to write it to a binary file in the same hex representation. I first converted the string to an integer using <code>sstrtoul()</code> function. Then
  I use <code>fwrite()</code> function to write that to a file. Here is the code that I wrote. I get the following output in my file after running this:</p>
  
  <blockquote>
    <p>6e03 0000 </p>
  </blockquote>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;fstream&gt;
  #include &lt;string&gt;
  
  using namespace std;
  
  int main() {
    ofstream fileofs("binary.bin", ios::binary | ios::out);
    string s = "036e";
    int x = strtoul(s.c_str(), NULL, 16);
    fileofs.write((char*)&amp;x, sizeof(int));
    fileofs.close();
  }
  </code></pre>
  
  <p>While the result that I expect is something like this:</p>
  
  <blockquote>
    <p>036e</p>
  </blockquote>
  
  <p>Can anybody explain exactly what I'm doing wrong over here?</p>