Skip to content

Instantly share code, notes, and snippets.

@bear454
Created February 25, 2014 21:18
Show Gist options
  • Save bear454/9217997 to your computer and use it in GitHub Desktop.
Save bear454/9217997 to your computer and use it in GitHub Desktop.
.net is *evil*: example.
# Convert a string in the expected format, into 16 bytes, emulating .Net's
# Guid constructor.
# "00000000-0000-0000-0000-000000000000" =>
# [CLSCompliantAttribute(false)]
# public Guid(
# uint a,
# ushort b,
# ushort c,
# byte d,
# byte e,
# byte f,
# byte g,
# byte h,
# byte i,
# byte j,
# byte k
# )
# => 16B
def dot_net_guid(string)
pattern = /\A([0-9a-f]{8})-([0-9a-f]{4})-([0-9a-f]{4})-([0-9a-f]{4})-([0-9a-f]{12})\z/i
parts = pattern.match(string)
raise ArgumentError unless parts
guid = "".encode("ASCII-8BIT")
guid << parts[1,1].pack("H*").unpack("l>").pack("l") +
parts[2,1].pack("H*").unpack("s>").pack("s") +
parts[3,1].pack("H*").unpack("s>").pack("s") +
parts[4,2].pack("H*H*")
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment