Created
September 17, 2015 23:38
-
-
Save jplesperance/1874d4d435323181aab8 to your computer and use it in GitHub Desktop.
encode/decode unicode string to 8-bit values
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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