The most memory efficient way to store base64 data in Python?

Suppose you have an MD5 hash encoded in base64. Then each character only needs 6 bits to store each character in the final 22-byte string (excluding the end of '=='). Thus, each base64 md5 hash can be reduced to 6 * 22 = 132 bits, which requires 25% less memory compared to the original 8 * 22 = 176 bits.

Is there any Python module or function that allows you to store base64 data as described above?

+3
source share
4 answers

David gave an answer that works on all base64 lines.

Just use

base64.decodestring
in base64 module. I.e
import base64
binary = base64.decodestring (base64_string)

- A more efficient representation of the memory of the original base64 string. if you truncate ending with '==' in your base64 md5, use it like

base64.decodestring (md5 + '==')
+1
source

The most efficient way to store base64 encoded data is to decode it and save it as a binary file. base64 is a transport encoding - it makes no sense to store data in it, especially in memory, if you have no good reason.

, nitpick: - - . - . , md5, sha hashlib, - - .digest() .hexdigest() -.

+7

base64 :

>>> b64 = "COIC09jwcwjiciOEIWIUNIUNE9832iun"
>>> len(b64)
32
>>> b = b64.decode("base64")
>>> b
'\x08\xe2\x02\xd3\xd8\xf0s\x08\xe2r#\x84!b\x144\x85\r\x13\xdf7\xda+\xa7'
>>> len(b)
24
+4

" base64"

.

. . . Base64. .

Base64 -, .

-. base64 -.

+2

Source: https://habr.com/ru/post/1758508/


All Articles