Skip to content

Instantly share code, notes, and snippets.

@ksss
Created June 21, 2017 08:07
Show Gist options
  • Save ksss/902c30f04cee540ad940ce8f57c2ffbe to your computer and use it in GitHub Desktop.
Save ksss/902c30f04cee540ad940ce8f57c2ffbe to your computer and use it in GitHub Desktop.
uuid pack/unpack
def packuuid(uuid)
uuid.split('-').pack("H8H4H4H4H12")
end
def unpackuuid(byte)
byte.unpack("H8H4H4H4H12").join('-')
end
uuid = "43e1254f-2d13-4193-b4b4-b9520ef0e9f3"
p uuid.length #=> 36
packed = packuuid(uuid)
p packed #=> "C\xE1%O-\x13A\x93\xB4\xB4\xB9R\x0E\xF0\xE9\xF3"
p packed.length #=> 16
p unpackuuid(packed) #=> "43e1254f-2d13-4193-b4b4-b9520ef0e9f3"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment