title: Guid to Binary 16 back and forth PHP
body: <p>I'm storing a guid(that actually points to a filename) as a Binary 16 in mysql. I'm using this function to generate the guid</p>
  
  <pre><code>private function guid(){
  
      if (function_exists('com_create_guid') === true) {
          return trim(com_create_guid(), '{}');
      }
  
      return sprintf('%04X%04X-%04X-%04X-%04X-%04X%04X%04X', mt_rand(0, 65535), mt_rand(0, 65535), 
          mt_rand(0, 65535), mt_rand(16384, 20479), mt_rand(32768, 49151), mt_rand(0, 65535), 
          mt_rand(0, 65535), mt_rand(0, 65535));
  }
  </code></pre>
  
  <p>I'm saving the guid as binary using the function</p>
  
  <pre><code>$binary =  pack("h*", str_replace('-', '', $guid ));
  </code></pre>
  
  <p>The problem is now when converting the binary field from the database back to guid to link  the file, I dont get the original guid generated by the guid function above, I use this mysql statement to convert the binary 16 back to guid:</p>
  
  <pre><code>CONCAT(
      HEX(SUBSTRING(hash,4,1)), HEX(SUBSTRING(hash,3,1)),
      HEX(SUBSTRING(hash,2,1)), HEX(SUBSTRING(hash,1,1)) , '-', 
      HEX(SUBSTRING(hash,6,1)),HEX(SUBSTRING(hash,5,1)),'-',
      HEX(SUBSTRING(hash,8,1)) , HEX(SUBSTRING(hash,7,1)),'-',
      HEX(SUBSTRING(hash,9,2)),'-',HEX(SUBSTRING(hash,11,6))
  )
  </code></pre>
  
  <p>where hash is the binary 16 field. Any Clue?</p>
  
  <p>Thanks.</p>