title: Convert UUID to bytes
body: <p>I'm trying to take a <code>uuid</code> and convert it back into the bytes it was generated from.</p>
  
  <p>I've been studying the <code>SecureRandom</code> source to see if I can reverse engineer the UUID back into bytes, but I'm having a hard time with it.</p>
  
  <p>What I need to do is basically the inverse of this:</p>
  
  <pre><code>def self.uuid
    ary = self.random_bytes(16).unpack("NnnnnN")
    ary[2] = (ary[2] &amp; 0x0fff) | 0x4000
    ary[3] = (ary[3] &amp; 0x3fff) | 0x8000
    "%08x-%04x-%04x-%04x-%04x%08x" % ary
  end
  </code></pre>
  
  <p>So I have this uuid:</p>
  
  <pre><code>"4b6d2066-78ac-49db-b8c4-9f58d8e8842f"
  </code></pre>
  
  <p>Which started out as this string of bytes:</p>
  
  <pre><code>"Km fx\xAC\xC9\xDB\xF8\xC4\x9FX\xD8\xE8\x84/"
  </code></pre>
  
  <p>Which was this unpacked:</p>
  
  <pre><code>[
      [0] 1265442918,
      [1] 30892,
      [2] 51675,
      [3] 63684,
      [4] 40792,
      [5] 3639116847
  ]
  </code></pre>
  
  <p>And this after <code>ary[2]</code> and <code>ary[3]</code> where modified</p>
  
  <pre><code>[
      [0] 1265442918,
      [1] 30892,
      [2] 18907,
      [3] 47300,
      [4] 40792,
      [5] 3639116847
  ]
  </code></pre>
  
  <p>So I got the original uuid split back out into its unjoined parts:</p>
  
  <pre><code>[
      [0] "4b6d2066",
      [1] "78ac",
      [2] "49db",
      [3] "b8c4",
      [4] "9f58",
      [5] "d8e8842f"
  ]
  </code></pre>
  
  <p>The problem I'm having is I'm not sure what the inverse of these 2 lines are:</p>
  
  <pre><code>ary[2] = (ary[2] &amp; 0x0fff) | 0x4000
  ary[3] = (ary[3] &amp; 0x3fff) | 0x8000
  </code></pre>
  
  <p>And I'm also not sure how to turn the remaining elements back into their integer values.  I'm sure it is some form of <code>pack</code> or <code>unpack</code>, but I'm not sure what it needs.</p>