Skip to content

Instantly share code, notes, and snippets.

@smartinov
Last active January 5, 2016 14:16
Show Gist options
  • Save smartinov/fe69caed43020311dbb7 to your computer and use it in GitHub Desktop.
Save smartinov/fe69caed43020311dbb7 to your computer and use it in GitHub Desktop.
Microsoft Python HexToGuid and GuidToHex
def hex_to_guid(hex):
swapper = lambda A, n=2: [A[i:i + n] for i in range(0, len(A), n)]
segments = [hex[0:8], hex[8:12], hex[12:16], hex[16:20], hex[20:]]
for i in xrange(3):
segments[i] = "".join(list(reversed(swapper(segments[i]))))
return "-".join(segments)
def guid_to_hex(guid):
swapper = lambda A, n=2: [A[i:i + n] for i in range(0, len(A), n)]
segments = [guid[0:8], guid[9:13], guid[14:18], guid[19:23], guid[24:]]
for i in xrange(3):
segments[i] = "".join(list(reversed(swapper(segments[i]))))
return ''.join(segments)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment