title: Is there any "standard" htonl-like function for 64 bits integers in C++?
body: <p>I'm working on an implementation of the memcache protocol which, at some points, uses 64 bits integer values. These values must be stored in "network byte order".</p>
  
  <p>I wish there was some <code>uint64_t htonll(uint64_t value)</code> function to do the change, but unfortunately, if it exist, I couldn't find it.</p>
  
  <p>So I have 1 or 2 questions:</p>
  
  <ul>
  <li>Is there any <strong>portable</strong> (Windows, Linux, AIX) standard function to do this ?</li>
  <li>If there is no such function, how would you implement it ?</li>
  </ul>
  
  <p>I have in mind a basic implementation but I don't know how to check the endianness at compile-time to make the code portable. So your help is more than welcome here ;)</p>
  
  <p>Thank you.</p>
  
  <hr/>
  
  <p>Here is the final solution I wrote, thanks to Brian's solution.</p>
  
  <pre><code>uint64_t htonll(uint64_t value)
  {
      // The answer is 42
      static const int num = 42;
  
      // Check the endianness
      if (*reinterpret_cast&lt;const char*&gt;(&amp;num) == num)
      {
          const uint32_t high_part = htonl(static_cast&lt;uint32_t&gt;(value &gt;&gt; 32));
          const uint32_t low_part = htonl(static_cast&lt;uint32_t&gt;(value &amp; 0xFFFFFFFFLL));
  
          return (static_cast&lt;uint64_t&gt;(low_part) &lt;&lt; 32) | high_part;
      } else
      {
          return value;
      }
  }
  </code></pre>