title: how to change byte order of 128 bit number
body: <p>I have an array of unsigned char that represents a 128 bit number in network byte order. How would I go about converting this to host byte order efficiently (in this case x86_64)?</p>
  
  <p>There don't seem to be any macros available in endian.h and my attempt at converting the upper 64 bits and lower 64 bits independently didn't work. The only method I've found that definitely works is a loop like so: </p>
  
  <pre><code>unsigned __int128 num = 0;
  for (int i = 0; i &lt; 16; i++) {
      num = (num &lt;&lt; 8) | byte[i];
  }
  </code></pre>
  
  <p>I ended up doing the follow:</p>
  
  <pre><code>union {
      unsigned char b[MD5_DIGEST_LENGTH];
      uint64_t d[2];
      unsigned __int128 q;
  } digest;
  MD5((const unsigned char *)str, length, digest.b);
  uint64_t tmp = digest.d[0];
  digest.d[0] = be64toh(digest.d[1]);
  digest.d[1] = be64toh(tmp);
  /* digest.q is now in native byte order */
  </code></pre>