Skip to content

Instantly share code, notes, and snippets.

@anilkeshwani
Last active October 19, 2024 21:26
Show Gist options
  • Select an option

  • Save anilkeshwani/f27bfc83d7807719e21790442c2b017d to your computer and use it in GitHub Desktop.

Select an option

Save anilkeshwani/f27bfc83d7807719e21790442c2b017d to your computer and use it in GitHub Desktop.
Decoding output from hexdump (hexadecimal integers) and converting to binary
#!/usr/bin/env python
hexstr = "53 61 6c 74 65 64 5f 5f" # ef bf bd ef bf bd ef bf
hexstr = hexstr.replace(" ", "")
print(len(str(hexstr)))
print(bytes.fromhex(hexstr).decode("utf-8"))
decimal = int(hexstr, 16)
# print(bin(decimal))
print(str(bin(decimal))[2:])
print(len(str(bin(decimal))) - 2) # -2 for 0b
print("=" * 50)
hexstr = "53"
print(len(str(hexstr)))
print(bytes.fromhex(hexstr).decode("utf-8"))
decimal = int(hexstr, 16)
# print(bin(decimal))
print(str(bin(decimal))[2:])
print(len(str(bin(decimal))) - 2) # -2 for 0b
print("=" * 50)
hexstr = "f" * 4
print(len(str(hexstr)))
# print(bytes.fromhex(hexstr).decode("utf-8"))
# UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 0: invalid start byte
decimal = int(hexstr, 16)
# print(bin(decimal))
print(str(bin(decimal))[2:])
print(len(str(bin(decimal))) - 2) # -2 for 0b
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment