Skip to content

Instantly share code, notes, and snippets.

@rcx
Created November 16, 2018 02:26
Show Gist options
  • Save rcx/a86703da453bbf3406f9b67c6e22edc4 to your computer and use it in GitHub Desktop.
Save rcx/a86703da453bbf3406f9b67c6e22edc4 to your computer and use it in GitHub Desktop.
python3 encoding cheatsheet
#!/usr/bin/env python3.6
my_string = 'hello world'
# get bytes from string
my_bytes = my_string.encode() # default is utf8. accepts: utf-8, utf16, ascii, etc
print(my_bytes)
# get hex from bytes
my_hex = my_bytes.hex() # NEW in python3.5, on python<3.4 use binascii (un)hexlify
print(my_hex)
# get string from hex
my_string_again = bytes.fromhex(my_hex).decode() # default encoding is utf8
print(my_string_again)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment