Skip to content

text.encoding

text as bytes and back: base64, URL and hex encodings, Unicode code points, hex dumps

Text as bytes and back. Every character has a number called its Unicode code point (A is 65, é is 233), and UTF-8, the encoding almost everything uses, stores that number in 1 to 4 bytes. So a string’s length in characters and in bytes can differ: “é” is one character but two bytes.

The bytes type, written b"...", holds raw bytes that aren’t necessarily text. Inside, \x and two hex digits gives any byte. Indexing gives a byte’s value as an integer, + joins bytes and * repeats them. .str decodes UTF-8 bytes into a string and .hex shows them as hex digits. Bytes longer than 32 are shown as a hex dump.

Tags: encoding

  • base64 encode a string’s UTF-8 bytes in base64, the most common way to send binary data as plain text, in emails, URLs or JSON
  • base32 encode a string’s UTF-8 bytes in base32, which avoids mixing upper and lower case
  • base58 encode a string’s UTF-8 bytes in base58, which avoids look-alike characters such as 0 and O, as Bitcoin addresses do
  • url_encode make a string safe to put in a URL, replacing other characters with % and their hex code, like %20 for a space
  • unicode each character’s code point in the standard U+ form, like U+00E9 for é
  • encode encode bytes, or a string’s UTF-8 bytes, in the named format
  • decode decode text in a format encode writes, the reverse of encode
  • decode_auto try every format decode knows, plus rot13, bytes written as binary, octal or decimal numbers, and single-byte XOR, layer on layer up to depth deep, and list the text that comes out, the most English-looking first
  • flag_find find CTF flags, prefix up to the next }, in bytes or text: as they are, and in what decode_auto’s decodings make of each run of printable text, up to 3 layers deep
  • from_byte_text bytes written out as numbers separated by spaces or commas, in binary, octal, decimal or hex
  • ord the Unicode code point (number) of a single character
  • chr the character with the given Unicode code point
  • bytes a string’s UTF-8 bytes, bytes made from a list of integers 0-255, or an integer’s bytes
  • int bytes read as one unsigned integer of any length, big-endian (most significant first) unless “little”
  • pkcs7_pad bytes padded to a multiple of size, 16 by default, with PKCS#7: n copies of the byte n, from 1 to size
  • pkcs7_unpad bytes with their PKCS#7 padding removed, checking it’s valid for blocks of size, 16 by default
  • from_bytes a string from UTF-8 bytes, given as bytes or as a list of integers
  • byte_len how many bytes the text takes in UTF-8, rather than how many characters it has
  • hexdump bytes shown as a hex dump, like the xxd tool, or just n bytes around address at
  • diff what changed from bytes a to b, by hex dump row, or nil if nothing did
  • bindiff what changed from bytes a to b as a table of offset, size, old and new, a row per run of changed bytes
  • find the offset of the first match of needle in bytes, or nil
  • find_all the offset of every match of needle in bytes, overlapping ones too
  • patch a copy of bytes with new written over them at offset
  • strings the readable text hidden in binary data: every run of at least min printable characters, like the Unix tool
  • entropy how unpredictable the bytes are (Shannon entropy)
1
# bytes
2
b"\x7fELF\n"
Example outputCtrl+Enter to run
1
# b[i] is one byte's value, b[a..b] a slice of bytes
2
b"\x7fELF"[1..]b"ELF"
3
# + joins bytes, * repeats them
4
b"ab" + b"\xff" * 2b"ab\xff\xff"
5
# .hex shows bytes as hex digits
6
b"hi".hex"6869"
7
# to base64, to base32, to base58, to hex or to url encodes
8
"hi" to base64"aGk="
9
# to unicode shows code points
10
"é" to unicode"U+00E9"
Example outputCtrl+Enter to run
base64(s: str) -> str

encode a string’s UTF-8 bytes in base64, the most common way to send binary data as plain text, in emails, URLs or JSON

Same as s to base64

1
base64("hi there")"aGkgdGhlcmU="
2
"hi" to base64"aGk="
Example outputCtrl+Enter to run

See also encode, decode

base32(s: str) -> str

encode a string’s UTF-8 bytes in base32, which avoids mixing upper and lower case

Same as s to base32

1
"hi" to base32"NBUQ===="
Example outputCtrl+Enter to run

See also encode, decode

base58(s: str) -> str

encode a string’s UTF-8 bytes in base58, which avoids look-alike characters such as 0 and O, as Bitcoin addresses do

Same as s to base58

1
"hi" to base58"8wr"
Example outputCtrl+Enter to run

See also encode, decode

url_encode(s: str) -> str

make a string safe to put in a URL, replacing other characters with % and their hex code, like %20 for a space

Same as s to url

1
"a b&c" to url"a%20b%26c"
Example outputCtrl+Enter to run

See also encode, decode, url_build

unicode(v: str|int) -> str

each character’s code point in the standard U+ form, like U+00E9 for é

Same as v to unicode

1
"é" to unicode"U+00E9"
2
233 to unicode"U+00E9"
Example outputCtrl+Enter to run

See also ord, chr

encode(s: str|bytes, fmt: str) -> str

encode bytes, or a string’s UTF-8 bytes, in the named format

The formats are “base64”, “base32”, “base58”, “base62”, “base85” (RFC 1924, as Python and git write it), “ascii85” (Adobe’s), “base91”, “uuencode”, “quoted_printable”, “punycode” (a domain name), “url” and “hex”

1
"hi there".encode("base64")"aGkgdGhlcmU="
2
"a b&c".encode("url")"a%20b%26c"
3
b"\xff\0".encode("ascii85")"rr<"
4
"bücher.de".encode("punycode")"xn--bcher-kva.de"
Example outputCtrl+Enter to run

See also decode

decode(s: str, fmt: str) -> str|bytes

decode text in a format encode writes, the reverse of encode

fmt is one of the formats listed under encode. If the result isn’t valid text, it comes back as bytes

1
"aGk=".decode("base64")"hi"
2
"6869".decode("hex")"hi"
3
"Cn8eVZg".decode("base58")"hello"
4
"xn--bcher-kva.de".decode("punycode")"bücher.de"
Example outputCtrl+Enter to run

See also encode, from_byte_text

decode_auto(s: str|bytes, depth: int = 3) -> table

try every format decode knows, plus rot13, bytes written as binary, octal or decimal numbers, and single-byte XOR, layer on layer up to depth deep, and list the text that comes out, the most English-looking first

Returns a table of chain, the decodings in the order applied, and result. Results that aren’t printable text are left out, and a result found again by a longer chain is listed once

1
decode_auto("Njg2OTIwNzQ2ODY1NzI2NQ==")[0]{chain: "base64 → hex", result: "hi there"}
2
decode_auto("uryyb jbeyq").result["hello world"]
Example outputCtrl+Enter to run

See also decode, from_byte_text, xor_solve

flag_find(b: str|bytes, prefix: str = “flag{”, *, base?: bool|int) -> table

find CTF flags, prefix up to the next }, in bytes or text: as they are, and in what decode_auto’s decodings make of each run of printable text, up to 3 layers deep

Returns a table of offset (where the flag, or the encoded text it came from, starts), chain (the decodings, empty for a flag found as it is) and flag. Each flag is listed once. With base, offsets start at that address instead of 0, and base: true means 0x400000

  • base the address the first byte is at, true for 0x400000
1
flag_find("junk ZmxhZ3toaWRkZW59 junk")table([{offset: 0x5, chain: "base64", flag: "flag{hidden}"}])
2
flag_find(b"\0\0CTF{raw}\0", "CTF\{")table([{offset: 0x2, chain: "", flag: "CTF{raw}"}])
Example outputCtrl+Enter to run

See also decode_auto, strings

from_byte_text(s: str, radix?: int) -> str|bytes

bytes written out as numbers separated by spaces or commas, in binary, octal, decimal or hex

Without a radix, it tries each one the numbers fit and keeps the result that reads most like English. If the result isn’t valid text, it comes back as bytes

1
from_byte_text("01001000 01101001")"Hi"
2
from_byte_text("110 151")"Hi"
3
from_byte_text("72, 105", 10)"Hi"
Example outputCtrl+Enter to run

See also decode, bytes

ord(c: str) -> int

the Unicode code point (number) of a single character

1
"A".ord65
2
"A".ord to hex0x41
Example outputCtrl+Enter to run

See also chr, bytes

chr(n: int) -> str

the character with the given Unicode code point

1
97.chr"a"
2
(65..70).map(chr).join"ABCDE"
Example outputCtrl+Enter to run

See also ord

bytes(v: str|list|bytes) -> bytes / bytes(n: int, order: str = “big”) -> bytes

a string’s UTF-8 bytes, bytes made from a list of integers 0-255, or an integer’s bytes

.str turns UTF-8 bytes back into a string. An integer gives as few bytes as it needs, big-endian (most significant first) as cryptography writes them, or little-endian with “little”, and int turns them back. For fixed-width little-endian values, like addresses, use p32 or p64

1
"hé".bytesb"h\xc3\xa9"
2
[0x7f, 69].bytesb"\x7fE"
3
"hi".bytes.str"hi"
4
bytes(0x414243)b"ABC"
5
bytes(258, "little")b"\x02\x01"
6
bytes(2 ** 64)b"\x01\0\0\0\0\0\0\0\0"
Example outputCtrl+Enter to run

See also from_bytes, read_bytes, ord, int, p64

int(b: bytes, order: str = “big”) -> int

bytes read as one unsigned integer of any length, big-endian (most significant first) unless “little”

The reverse of bytes(n). For fixed-width little-endian values use u32 or u64

1
int(b"ABC")4276803
2
int(b"\x02\x01", "little")258
3
int("flag".bytes) to hex0x666c6167
Example outputCtrl+Enter to run

See also bytes, u64

pkcs7_pad(b: bytes, size?: int) -> bytes

bytes padded to a multiple of size, 16 by default, with PKCS#7: n copies of the byte n, from 1 to size

A block cipher like AES needs whole blocks, so a full block of padding is added when the length already fits

1
pkcs7_pad(b"YELLOW SUBMARINE", 20)b"YELLOW SUBMARINE\x04\x04\x04\x04"
2
pkcs7_pad(b"hi").len16
Example outputCtrl+Enter to run

See also pkcs7_unpad

pkcs7_unpad(b: bytes, size?: int) -> bytes

bytes with their PKCS#7 padding removed, checking it’s valid for blocks of size, 16 by default

Bad padding is an error, the check a padding oracle attack relies on

1
pkcs7_unpad(b"YELLOW SUBMARINE\x04\x04\x04\x04", 20)b"YELLOW SUBMARINE"
Example outputCtrl+Enter to run

See also pkcs7_pad

from_bytes(v: list|bytes) -> str

a string from UTF-8 bytes, given as bytes or as a list of integers

1
[104, 105].from_bytes"hi"
Example outputCtrl+Enter to run

See also bytes, chr

byte_len(v: str|list|bytes) -> int

how many bytes the text takes in UTF-8, rather than how many characters it has

1
"héllo".byte_len6
Example outputCtrl+Enter to run

See also len, bytes

hexdump(v: str|list|bytes, at?: int|str, n?: int|str, *, plain: bool = false, text: bool = false, base?: bool|int) -> str

bytes shown as a hex dump, like the xxd tool, or just n bytes around address at

Also called xxd.

Each line has the offset, 16 bytes in hex, and the same bytes as text with dots for unprintable ones. With at, the lines keep their real offsets and the byte at at is highlighted, with n bytes shown (64 if not given). Bytes are colored by kind on a terminal: null, printable, whitespace and control, and non-ASCII With text, only the text column is shown, as one line, so it can be selected and copied cleanly With base, the offsets shown start at that address instead of 0, and base: true means 0x400000. at and n still count bytes of the data

1
hexdump("hi\n")"00000000: 6869 0a hi."
2
hexdump([0, 255, 65])"00000000: 00ff 41 ..A"
3
hexdump(b"\x00" * 40 + b"ELF", 41, 16)"00000020: 0000 0000 0000 0000 454c 46 ........ELF"
4
hexdump("hello world", text: true)"hello world"
Example outputCtrl+Enter to run

As a command:

  • -p, --plain no colors, even on a terminal
  • -t, --text only the text column, as one line
  • -B, --base the address the first byte is shown at, true for 0x400000
xxd a.out 0x1000
# → xxd(read_bytes("a.out"), "0x1000")

See also bytes, entropy, read_bytes

diff(a: bytes, b: bytes, *, plain: bool = false, table: bool = false, base?: bool|int) -> any

what changed from bytes a to b, by hex dump row, or nil if nothing did

Changes come in hunks headed @@ with their byte offset and length, with 3 unchanged rows around them. Removed rows start with - and added rows with +, colored on a terminal with the changed bytes picked out. With the table flag, it is instead a table with a row per byte that differs, comparing position by position with nil past the end of the shorter. With base, offsets start at that address instead of 0, and base: true means 0x400000

1
diff(b"\x7fELF\x02\xff", b"\x7fELF\x01\xff")
@@ -0x0,6 +0x0,6 @@ - 00000000: 7f45 4c46 02ff .ELF.. + 00000000: 7f45 4c46 01ff .ELF..
2
diff(b"\x7fELF", b"\x7fELG\x00", table: true)table([{offset: 0x3, a: 0x46, b: 0x47}, {offset: 0x4, a: nil, b: 0x0}])
3
diff(b"ELF", b"ELG", table: true, base: true)table([{offset: 0x400002, a: 0x46, b: 0x47}])
Example outputCtrl+Enter to run

As a command:

  • -p, --plain no colors, even on a terminal
  • -t, --table a table of differing bytes instead of hunks
  • -B, --base the address the first byte is at, true for 0x400000
diff -b a.out b.out
# → diff(read_bytes("a.out"), read_bytes("b.out"))

See also hexdump

bindiff(a: bytes, b: bytes, *, base?: bool|int) -> table

what changed from bytes a to b as a table of offset, size, old and new, a row per run of changed bytes

Bytes compare position by position, and the extra bytes of the longer one are one more run. With base, offsets start at that address instead of 0, and base: true means 0x400000

  • base the address the first byte is at, true for 0x400000
1
bindiff(b"\x7fELF\x01\x01", b"\x7fELG\x02\x01\x00")table([{offset: 0x3, size: 2, old: b"F\x01", new: b"G\x02"}, {offset: 0x6, size: 1, old: b"", new: b"\0"}])
Example outputCtrl+Enter to run

See also diff, patch

find(b: bytes, needle: bytes|str) -> any|nil

the offset of the first match of needle in bytes, or nil

The needle is bytes, or a hex pattern where ?? matches any byte, like “55 48 89 e5 ?? ??”

1
b"\x00\x7fELF".find(b"ELF")0x2
2
b"\x55\x48\x89\xe5".find("48 ?? e5")0x1
Example outputCtrl+Enter to run

See also find_all, patch

find_all(b: bytes, needle: bytes|str) -> list

the offset of every match of needle in bytes, overlapping ones too

The needle is bytes, or a hex pattern where ?? matches any byte

1
b"abcabc".find_all(b"bc")[0x1, 0x4]
2
b"\x01\x02\x01\x03".find_all("01 ??")[0x0, 0x2]
Example outputCtrl+Enter to run

See also find, patch

patch(b: bytes, offset: int, new: bytes) -> any

a copy of bytes with new written over them at offset

It’s an error if new runs past the end. Save the result with write_file

1
patch(b"\x74\x05\x90", 0, b"\xeb")b"\xeb\x05\x90"
Example outputCtrl+Enter to run

See also find, bindiff

strings(v: bytes|str, min: int = 4, *, table: bool = false, base?: bool|int) -> any

the readable text hidden in binary data: every run of at least min printable characters, like the Unix tool

With the table flag, it is a table of offset and text. With base, offsets start at that address instead of 0, and base: true means 0x400000

1
b"\x00\x01hello\xffab\x00world!".strings["hello", "world!"]
2
strings(b"ab\x00cd", 2)["ab", "cd"]
3
strings(b"\x00hello\x00world", table: true)table([{offset: 0x1, text: "hello"}, {offset: 0x7, text: "world"}])
Example outputCtrl+Enter to run

As a command:

  • -t, --table a table of offset and text instead of a list
  • -B, --base the address the first byte is at, true for 0x400000
strings -t a.out
# → strings(read_bytes("a.out"), table: true)

See also hexdump, read_bytes

entropy(v: str|list|bytes) -> float

how unpredictable the bytes are (Shannon entropy)

It goes from 0 bits per byte when every byte is the same to 8 for random or encrypted data

1
"aaaa".entropy0
2
"abcd".entropy2
Example outputCtrl+Enter to run

See also hexdump

1
# base64
2
"hi" to base64"aGk="
3
# URL encoding
4
"a b&c".encode("url")"a%20b%26c"
5
# base58, as in Bitcoin
6
"hello".encode("base58")"Cn8eVZg"
7
# an internationalized domain name as DNS sees it
8
"münchen.de".encode("punycode")"xn--mnchen-3ya.de"
9
# bytes written out in binary
10
from_byte_text("01101000 01101001")"hi"
11
# UTF-8 bytes
12
"héllo".bytesb"h\xc3\xa9llo"
13
# the character for code point 9731
14
chr(9731)"☃"
15
# an emoji is 4 bytes but 1 character
16
["😀".byte_len, "😀".len][4, 1]
17
# every byte of a string
18
hexdump("héllo\tworld\n")"00000000: 68c3 a96c 6c6f 0977 6f72 6c64 0a h..llo.world."
19
# how random is it? 0 is not at all
20
"aaaaaaaa".entropy0
Example outputCtrl+Enter to run

Example results generated on 2026-11-04. Ones using now, today or randomness will differ when you run them.