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.
writing
Section titled “writing”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
base64encode a string’s UTF-8 bytes in base64, the most common way to send binary data as plain text, in emails, URLs or JSONbase32encode a string’s UTF-8 bytes in base32, which avoids mixing upper and lower casebase58encode a string’s UTF-8 bytes in base58, which avoids look-alike characters such as 0 and O, as Bitcoin addresses dourl_encodemake a string safe to put in a URL, replacing other characters with % and their hex code, like %20 for a spaceunicodeeach character’s code point in the standard U+ form, like U+00E9 for éencodeencode bytes, or a string’s UTF-8 bytes, in the named formatdecodedecode text in a format encode writes, the reverse of encodedecode_autotry 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 firstflag_findfind 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 deepfrom_byte_textbytes written out as numbers separated by spaces or commas, in binary, octal, decimal or hexordthe Unicode code point (number) of a single characterchrthe character with the given Unicode code pointbytesa string’s UTF-8 bytes, bytes made from a list of integers 0-255, or an integer’s bytesintbytes read as one unsigned integer of any length, big-endian (most significant first) unless “little”pkcs7_padbytes padded to a multiple of size, 16 by default, with PKCS#7: n copies of the byte n, from 1 to sizepkcs7_unpadbytes with their PKCS#7 padding removed, checking it’s valid for blocks of size, 16 by defaultfrom_bytesa string from UTF-8 bytes, given as bytes or as a list of integersbyte_lenhow many bytes the text takes in UTF-8, rather than how many characters it hashexdumpbytes shown as a hex dump, like the xxd tool, or just n bytes around address atdiffwhat changed from bytes a to b, by hex dump row, or nil if nothing didbindiffwhat changed from bytes a to b as a table of offset, size, old and new, a row per run of changed bytesfindthe offset of the first match of needle in bytes, or nilfind_allthe offset of every match of needle in bytes, overlapping ones toopatcha copy of bytes with new written over them at offsetstringsthe readable text hidden in binary data: every run of at least min printable characters, like the Unix toolentropyhow unpredictable the bytes are (Shannon entropy)
1# bytes2b"\x7fELF\n"syntax
Section titled “syntax”1# b[i] is one byte's value, b[a..b] a slice of bytes2b"\x7fELF"[1..]b"ELF"3# + joins bytes, * repeats them4b"ab" + b"\xff" * 2b"ab\xff\xff"5# .hex shows bytes as hex digits6b"hi".hex"6869"7# to base64, to base32, to base58, to hex or to url encodes8"hi" to base64"aGk="9# to unicode shows code points10"é" to unicode"U+00E9"Functions
Section titled “Functions”base64
Section titled “base64”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
1base64("hi there")"aGkgdGhlcmU="2"hi" to base64"aGk="base32
Section titled “base32”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===="base58
Section titled “base58”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"url_encode
Section titled “url_encode”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"See also encode, decode, url_build
unicode
Section titled “unicode”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"2233 to unicode"U+00E9"encode
Section titled “encode”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"3b"\xff\0".encode("ascii85")"rr<"4"bücher.de".encode("punycode")"xn--bcher-kva.de"See also decode
decode
Section titled “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"See also encode, from_byte_text
decode_auto
Section titled “decode_auto”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
1decode_auto("Njg2OTIwNzQ2ODY1NzI2NQ==")[0]{chain: "base64 → hex", result: "hi there"}2decode_auto("uryyb jbeyq").result["hello world"]See also decode, from_byte_text, xor_solve
flag_find
Section titled “flag_find”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
basethe address the first byte is at, true for 0x400000
1flag_find("junk ZmxhZ3toaWRkZW59 junk")table([{offset: 0x5, chain: "base64", flag: "flag{hidden}"}])2flag_find(b"\0\0CTF{raw}\0", "CTF\{")table([{offset: 0x2, chain: "", flag: "CTF{raw}"}])See also decode_auto, strings
from_byte_text
Section titled “from_byte_text”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
1from_byte_text("01001000 01101001")"Hi"2from_byte_text("110 151")"Hi"3from_byte_text("72, 105", 10)"Hi"ord(c: str) -> int
the Unicode code point (number) of a single character
1"A".ord652"A".ord to hex0x41chr(n: int) -> str
the character with the given Unicode code point
197.chr"a"2(65..70).map(chr).join"ABCDE"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"4bytes(0x414243)b"ABC"5bytes(258, "little")b"\x02\x01"6bytes(2 ** 64)b"\x01\0\0\0\0\0\0\0\0"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
1int(b"ABC")42768032int(b"\x02\x01", "little")2583int("flag".bytes) to hex0x666c6167pkcs7_pad
Section titled “pkcs7_pad”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
1pkcs7_pad(b"YELLOW SUBMARINE", 20)b"YELLOW SUBMARINE\x04\x04\x04\x04"2pkcs7_pad(b"hi").len16See also pkcs7_unpad
pkcs7_unpad
Section titled “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
1pkcs7_unpad(b"YELLOW SUBMARINE\x04\x04\x04\x04", 20)b"YELLOW SUBMARINE"See also pkcs7_pad
from_bytes
Section titled “from_bytes”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"byte_len
Section titled “byte_len”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_len6hexdump
Section titled “hexdump”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
1hexdump("hi\n")"00000000: 6869 0a hi."2hexdump([0, 255, 65])"00000000: 00ff 41 ..A"3hexdump(b"\x00" * 40 + b"ELF", 41, 16)"00000020: 0000 0000 0000 0000 454c 46 ........ELF"4hexdump("hello world", text: true)"hello world"As a command:
-p,--plainno colors, even on a terminal-t,--textonly the text column, as one line-B,--basethe 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
1diff(b"\x7fELF\x02\xff", b"\x7fELF\x01\xff")@@ -0x0,6 +0x0,6 @@
- 00000000: 7f45 4c46 02ff .ELF..
+ 00000000: 7f45 4c46 01ff .ELF..2diff(b"\x7fELF", b"\x7fELG\x00", table: true)table([{offset: 0x3, a: 0x46, b: 0x47}, {offset: 0x4, a: nil, b: 0x0}])3diff(b"ELF", b"ELG", table: true, base: true)table([{offset: 0x400002, a: 0x46, b: 0x47}])As a command:
-p,--plainno colors, even on a terminal-t,--tablea table of differing bytes instead of hunks-B,--basethe 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
Section titled “bindiff”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
basethe address the first byte is at, true for 0x400000
1bindiff(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"}])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 ?? ??”
1b"\x00\x7fELF".find(b"ELF")0x22b"\x55\x48\x89\xe5".find("48 ?? e5")0x1find_all
Section titled “find_all”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
1b"abcabc".find_all(b"bc")[0x1, 0x4]2b"\x01\x02\x01\x03".find_all("01 ??")[0x0, 0x2]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
1patch(b"\x74\x05\x90", 0, b"\xeb")b"\xeb\x05\x90"strings
Section titled “strings”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
1b"\x00\x01hello\xffab\x00world!".strings["hello", "world!"]2strings(b"ab\x00cd", 2)["ab", "cd"]3strings(b"\x00hello\x00world", table: true)table([{offset: 0x1, text: "hello"}, {offset: 0x7, text: "world"}])As a command:
-t,--tablea table of offset and text instead of a list-B,--basethe 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
Section titled “entropy”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".entropy02"abcd".entropy2See also hexdump
More examples
Section titled “More examples”encoding
Section titled “encoding”1# base642"hi" to base64"aGk="3# URL encoding4"a b&c".encode("url")"a%20b%26c"5# base58, as in Bitcoin6"hello".encode("base58")"Cn8eVZg"7# an internationalized domain name as DNS sees it8"münchen.de".encode("punycode")"xn--mnchen-3ya.de"9# bytes written out in binary10from_byte_text("01101000 01101001")"hi"11# UTF-8 bytes12"héllo".bytesb"h\xc3\xa9llo"13# the character for code point 973114chr(9731)"☃"15# an emoji is 4 bytes but 1 character16["😀".byte_len, "😀".len][4, 1]17# every byte of a string18hexdump("héllo\tworld\n")"00000000: 68c3 a96c 6c6f 0977 6f72 6c64 0a h..llo.world."19# how random is it? 0 is not at all20"aaaaaaaa".entropy0Example results generated on 2026-11-04. Ones using now, today or randomness will differ when you run them.