Quantcast
Channel: Planet Python
Viewing all articles
Browse latest Browse all 22462

Python Does What?!: Base64 vs UTF-8

$
0
0
A common pattern when dealing with JSON data is to base64 encode binary strings.  This is not strictly necessary, JSON strings can contain binary data (via UTF-8 escape sequences).

What is the size relationship for high-entropy (e.g. compressed) binary data?

>>> every_byte = ''.join([chr(i) for i in range(256)])
>>> import json
>>> import base64
>>> len(json.dumps(every_byte.decode('utf-8', 'replace')))
1045
>>> len(json.dumps(base64.b64encode(every_byte)))
346

Viewing all articles
Browse latest Browse all 22462

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>