Base64 in Python: Difference between revisions
Jump to navigation
Jump to search
(→Decode) |
|||
(2 intermediate revisions by the same user not shown) | |||
Line 3: | Line 3: | ||
=Overview= | =Overview= | ||
=Encode= | =Encode= | ||
==Binary Data== | |||
<syntaxhighlight lang='py'> | <syntaxhighlight lang='py'> | ||
import base64 | import base64 | ||
Line 8: | Line 9: | ||
c = base64.b64encode(binary_data).decode('utf-8') | c = base64.b64encode(binary_data).decode('utf-8') | ||
</syntaxhighlight> | </syntaxhighlight> | ||
==Strings== | |||
<syntaxhighlight lang='py'> | |||
import base64 | |||
sample_string = "something" | |||
sample_string_bytes = sample_string.encode("utf-8") | |||
base64_bytes = base64.b64encode(sample_string_bytes) | |||
base64_string = base64_bytes.decode("utf-8") | |||
</syntaxhighlight> | |||
=Decode= | =Decode= | ||
==Decode Binary Data== | |||
<syntaxhighlight lang='py'> | <syntaxhighlight lang='py'> | ||
import base64 | import base64 | ||
base64_text = ... | base64_text = ... | ||
binary_data = base64.b64decode(base64_text) | binary_data = base64.b64decode(base64_text) | ||
</syntaxhighlight> | |||
==Decode Text== | |||
<syntaxhighlight lang='py'> | |||
import base64 | |||
base64_encoded_text = ... | |||
text = base64.b64decode(base64_encoded_text.encode('utf-8')).decode('utf-8') | |||
</syntaxhighlight> | </syntaxhighlight> |
Latest revision as of 02:06, 26 July 2022
Internal
Overview
Encode
Binary Data
import base64
binary_data = ...
c = base64.b64encode(binary_data).decode('utf-8')
Strings
import base64
sample_string = "something"
sample_string_bytes = sample_string.encode("utf-8")
base64_bytes = base64.b64encode(sample_string_bytes)
base64_string = base64_bytes.decode("utf-8")
Decode
Decode Binary Data
import base64
base64_text = ...
binary_data = base64.b64decode(base64_text)
Decode Text
import base64
base64_encoded_text = ...
text = base64.b64decode(base64_encoded_text.encode('utf-8')).decode('utf-8')