title: How to convert binary to integer in mysql?
body: <p>To put my question into perspective:</p>
  
  <p>I have a PHP app, which stores IP's of users in a MySQL table.</p>
  
  <p>The column type is <code>VARBINARY(16)</code>, and the app uses PHP's <code>inet_pton</code> to form a binary string.</p>
  
  <p>That is the string has 4 bytes for a typical IP4 address.</p>
  
  <p>How to retrieve these IPs from the table, displaying them in a human readable form?</p>
  
  <p>My current solution is:</p>
  
  <pre><code>select INET_NTOA(CONV(HEX(ip),16,10)) from operation_ip;
  </code></pre>
  
  <p>Is there a more direct way to do that?</p>
  
  <p>In particular is <code>CONV(HEX(x),16,10)</code> the easiest way to change 4 bytes into an integer (actually I believe it is not even an integer, but a string which looks like integer).</p>
  
  <p>(I use <code>VARBINARY(16)</code>, as PHP's <code>inet_pton</code> can return 16-bytes for IPv6 addresses. AFAIK MySQL's <code>INET_NTOA</code> does not support IPv6, but at the moment this is not my biggest concern).</p>