Skip to content

Instantly share code, notes, and snippets.

@MayerDaniel
Created June 7, 2024 15:31
Show Gist options
  • Save MayerDaniel/78e88dec2f6fcc0b20ed377fba3d852c to your computer and use it in GitHub Desktop.
Save MayerDaniel/78e88dec2f6fcc0b20ed377fba3d852c to your computer and use it in GitHub Desktop.
guidconvert
import struct
import argparse
def hex_string_to_guid(hex_string):
# Convert the hex string to bytes
guid_bytes = bytes.fromhex(hex_string)
if len(guid_bytes) != 16:
print("Invalid GUID hex string length")
return None
# Convert the bytes into the human readable format
# Take note of which ints are little endian!
first = struct.unpack("<I", guid_bytes[:4])[0]
second = struct.unpack("<H", guid_bytes[4:6])[0]
third = struct.unpack("<H", guid_bytes[6:8])[0]
fourth = struct.unpack(">H", guid_bytes[8:10])[0]
fifth = ''.join('{:02x}'.format(x) for x in guid_bytes[10:])
# Adjust the format to include leading zeros where necessary
guid_string = f'{first:08x}-{second:04x}-{third:04x}-{fourth:04x}-{fifth}'
return guid_string
def main():
parser = argparse.ArgumentParser(description="Convert a hex string to a GUID")
parser.add_argument("hex_string", help="Hex string representing the GUID")
args = parser.parse_args()
guid = hex_string_to_guid(args.hex_string)
if guid:
print(guid)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment