title: How do I convert a 64bit integer to a char array and back?
body: <p>I am having trouble converting a int64_t to a char array and back. I don't know what is wrong with the code below, it makes complete logical sense to me. The code works for <code>a</code> as shown, but not the second number <code>b</code> which clearly falls into the range of int64_t. </p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdint.h&gt;
  
  void int64ToChar(char mesg[], int64_t num) {
    for(int i = 0; i &lt; 8; i++) mesg[i] = num &gt;&gt; (8-1-i)*8;
  }
  
  int64_t charTo64bitNum(char a[]) {
    int64_t n = 0;
    n = ((a[0] &lt;&lt; 56) &amp; 0xFF00000000000000U)
      | ((a[1] &lt;&lt; 48) &amp; 0x00FF000000000000U)
      | ((a[2] &lt;&lt; 40) &amp; 0x0000FF0000000000U)
      | ((a[3] &lt;&lt; 32) &amp; 0x000000FF00000000U)
      | ((a[4] &lt;&lt; 24) &amp; 0x00000000FF000000U)
      | ((a[5] &lt;&lt; 16) &amp; 0x0000000000FF0000U)
      | ((a[6] &lt;&lt;  8) &amp; 0x000000000000FF00U)
      | ( a[7]        &amp; 0x00000000000000FFU);
    return n;
  }
  
  int main(int argc, char *argv[]) {
    int64_t a = 123456789;
    char *aStr = new char[8];
    int64ToChar(aStr, a);
    int64_t aNum = charTo64bitNum(aStr);
    printf("aNum = %lld\n",aNum);
  
    int64_t b = 51544720029426255;
    char *bStr = new char[8];
    int64ToChar(bStr, b);
    int64_t bNum = charTo64bitNum(bStr);
    printf("bNum = %lld\n",bNum);
    return 0;
  }
  </code></pre>
  
  <p>output is</p>
  
  <pre><code>aNum = 123456789
  bNum = 71777215744221775
  </code></pre>
  
  <p>The code also gives two warnings that I don't know how to get rid of.</p>
  
  <pre><code>warning: integer constant is too large for ‘unsigned long’ type
  warning: left shift count &gt;= width of type
  </code></pre>