typestar

Encoding bytes as text in Python

base64 when bytes must survive a text channel, hex when humans read it.

import base64

raw = b"typestar\x00\xff"

encoded = base64.b64encode(raw)
print(encoded, base64.b64decode(encoded) == raw)
print(base64.urlsafe_b64encode(raw))

print(raw.hex(), bytes.fromhex(raw.hex()) == raw)
print(base64.b32encode(b"ab"))

How it works

  1. b64encode takes and returns bytes.
  2. urlsafe_b64encode avoids the characters a URL dislikes.
  3. bytes.hex and bytes.fromhex are the readable pair.

Keywords and builtins used here

The run, in numbers

Lines
10
Characters to type
241
Tokens
73
Three-star pace
110 tpm

At the three-star pace of 110 tokens a minute, this run takes about 40 seconds.

Type this snippet

Step 2 of 2 in Bytes & secrets, step 31 of 41 in Domain tools.

← Previous Next →