Skip to content

Instantly share code, notes, and snippets.

@jplesperance
Created September 17, 2015 23:38
Show Gist options
  • Select an option

  • Save jplesperance/1874d4d435323181aab8 to your computer and use it in GitHub Desktop.

Select an option

Save jplesperance/1874d4d435323181aab8 to your computer and use it in GitHub Desktop.
encode/decode unicode string to 8-bit values
def to_str(bytes_or_str):
if isinstance(bytes_or_str, bytes):
value = bytes_or_str.decode('utf-8')
else:
value = bytes_or_str
return value # Instance of str
def to_bytes(bytes_or_str):
if isinstance(bytes_or_str, str):
value = bytes_or_str.encode('utf-8')
else:
value = bytes_or_str
return value # Instance of bytes
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment