Python Cryptography Tools: Difference between revisions
Jump to navigation
Jump to search
No edit summary |
|||
Line 22: | Line 22: | ||
</pre> | </pre> | ||
Base64 uses 64 characters (and a padding symbol) to represent numbers in groups of six bits. These don't line up nicely with sequences of bytes. | [https://en.wikipedia.org/wiki/Base64 Base64] uses 64 characters (and a padding symbol) to represent numbers in groups of six bits. These don't line up nicely with sequences of bytes. | ||
Revision as of 23:52, 8 February 2024
Encoding
Integers can be encoded as either binary, decimal, hexadecimal, or Base64 (among others).
>>> bin(95616) '0b10111010110000000' >>> hex(95616) '0x17580' >>> import base64 >>> base64.b64encode(95616) TypeError: a bytes-like object is required, not 'int'
Converting to Bytes
The most straightforward way to convert number-like objects into bytes-like objects seems to involve converting to string first.
>>> bytes(bin(95616), 'ascii') b'0b10111010110000000' >>> bytes(hex(95616), 'ascii') b'0x17580' >>> base64.b64encode(bytes(str(95616), 'ascii')) b'OTU2MTY='
Base64 uses 64 characters (and a padding symbol) to represent numbers in groups of six bits. These don't line up nicely with sequences of bytes.