| name | encoding-formats |
| description | Encode, decode, and convert between data formats. Use when working with Base64, URL encoding, hex, Unicode, JWT tokens, hashing, checksums, or converting between serialization formats like JSON, MessagePack, and protobuf wire format. |
| metadata | {"clawdbot":{"emoji":"🔢","requires":{"anyBins":["base64","python3","openssl","xxd"]},"os":["linux","darwin","win32"]}} |
Encoding & Formats
Encode, decode, and inspect data in common formats. Covers Base64, URL encoding, hex, Unicode, JWTs, hashing, checksums, and serialization formats.
When to Use
- Decoding a Base64 string from an API response or config
- URL-encoding parameters for HTTP requests
- Inspecting hex dumps of binary data
- Decoding JWT tokens to see claims
- Computing or verifying file checksums
- Converting between character encodings (UTF-8, Latin-1, etc.)
- Understanding wire formats (protobuf, MessagePack)
Base64
Encode and decode
echo -n "Hello, World!" | base64
echo "SGVsbG8sIFdvcmxkIQ==" | base64 -d
base64 image.png > image.b64
cat file.bin | base64
base64 -d image.b64 > image.png
echo -n "Hello" | base64 | tr '+/' '-_' | tr -d '='
echo "SGVsbG8" | tr '-_' '+/' | base64 -d
In code
btoa('Hello');
atob('SGVsbG8=');
Buffer.from('Hello').toString('base64');
Buffer.from('SGVsbG8=', 'base64').toString();
Buffer.from(binaryData).toString('base64');
Buffer.from(b64String, 'base64');
import base64
base64.b64encode(b"Hello").decode()
base64.b64decode("SGVsbG8=")
base64.urlsafe_b64encode(b"Hello").decode()
base64.urlsafe_b64decode("SGVsbG8=")
URL Encoding
Encode and decode
python3 -c "from urllib.parse import quote; print(quote('hello world & foo=bar'))"
python3 -c "from urllib.parse import unquote; print(unquote('hello%20world%20%26%20foo%3Dbar'))"
curl -G --data-urlencode "q=hello world & more" https://api.example.com/search
In code
encodeURIComponent('hello world & foo=bar');
decodeURIComponent('hello%20world%20%26%20foo%3Dbar');
encodeURI('https://example.com/path?q=hello world');
encodeURIComponent('https://example.com/path?q=hello world');
from urllib.parse import quote, unquote, urlencode
quote('hello world')
unquote('hello%20world')
urlencode({'q': 'hello world', 'page': 1})
Hex
View and convert
xxd file.bin | head -20
xxd -l 64 file.bin
xxd -p file.bin
echo "48656c6c6f" | xxd -r -p
od -A x -t x1z file.bin | head -20
hexdump -C file.bin | head -20
python3 -c "print(bytes.fromhex('48656c6c6f').decode())"
python3 -c "print('Hello'.encode().hex())"
In code
Buffer.from('Hello').toString('hex');
Buffer.from('48656c6c6f', 'hex').toString();
(255).toString(16);
parseInt('ff', 16);
"Hello".encode().hex()
bytes.fromhex('48656c6c6f').decode()
hex(255)
int('ff', 16)
Unicode
Inspect characters
echo -n "Hello 世界" | python3 -c "
import sys
for char in sys.stdin.read():
print(f'U+{ord(char):04X} {char} {char.encode(\"utf-8\").hex()}')"
printf '\u0048\u0065\u006c\u006c\u006f'
echo -e '\xE4\xB8\x96\xE7\x95\x8C'
file -bi document.txt
Encoding conversion
iconv -f ISO-8859-1 -t UTF-8 input.txt > output.txt
iconv -f UTF-16 -t UTF-8 input.txt > output.txt
iconv -l
python3 -c "
with open('latin1.txt', 'r', encoding='iso-8859-1') as f:
content = f.read()
with open('utf8.txt', 'w', encoding='utf-8') as f:
f.write(content)
"
Common Unicode issues
BOM (Byte Order Mark):
UTF-8 BOM: EF BB BF at start of file
Remove: sed -i '1s/^\xEF\xBB\xBF//' file.txt
Normalization (NFC vs NFD):
"é" can be U+00E9 (one char) or U+0065 U+0301 (e + combining accent)
Python: import unicodedata; unicodedata.normalize('NFC', text)
Mojibake (wrong encoding):
"café" appears as "café" → file is UTF-8 but read as Latin-1
Fix: re-read with correct encoding
JWT (JSON Web Tokens)
Decode a JWT
TOKEN="eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c"
echo "$TOKEN" | cut -d. -f1 | tr '-_' '+/' | base64 -d 2>/dev/null | jq
echo "$TOKEN" | cut -d. -f2 | tr '-_' '+/' | base64 -d 2>/dev/null | jq
jwt_decode() {
echo "$1" | cut -d. -f2 | tr '-_' '+/' | base64 -d 2>/dev/null | jq
}
jwt_decode "$TOKEN"
In code
function decodeJWT(token) {
const [header, payload] = token.split('.').slice(0, 2)
.map(part => JSON.parse(atob(part.replace(/-/g, '+').replace(/_/g, '/'))));
return { header, payload };
}
function isJWTExpired(token) {
const { payload } = decodeJWT(token);
return payload.exp && payload.exp < Math.floor(Date.now() / 1000);
}
import json, base64
def decode_jwt(token):
parts = token.split('.')
def pad(s): return s + '=' * (4 - len(s) % 4)
header = json.loads(base64.urlsafe_b64decode(pad(parts[0])))
payload = json.loads(base64.urlsafe_b64decode(pad(parts[1])))
return header, payload
header, payload = decode_jwt(token)
Hashing
Common hash functions
echo -n "Hello" | md5sum
echo -n "Hello" | md5
echo -n "Hello" | sha256sum
echo -n "Hello" | shasum -a 256
echo -n "Hello" | sha1sum
echo -n "Hello" | sha512sum
sha256sum file.bin
md5sum file.bin
echo -n "Hello" | openssl dgst -sha256
openssl dgst -sha256 file.bin
In code
const crypto = require('crypto');
crypto.createHash('sha256').update('Hello').digest('hex');
const fs = require('fs');
const hash = crypto.createHash('sha256');
hash.update(fs.readFileSync('file.bin'));
console.log(hash.digest('hex'));
import hashlib
hashlib.sha256(b"Hello").hexdigest()
with open("file.bin", "rb") as f:
print(hashlib.sha256(f.read()).hexdigest())
Checksums for file integrity
sha256sum *.tar.gz > checksums.sha256
sha256sum -c checksums.sha256
sha256sum file1.bin file2.bin
cmp file1.bin file2.bin && echo "Identical" || echo "Different"
Serialization Formats
JSON ↔ other formats
python3 -c "import json, yaml, sys; yaml.dump(json.load(sys.stdin), sys.stdout)" < data.json
python3 -c "import json, yaml, sys; json.dump(yaml.safe_load(sys.stdin), sys.stdout, indent=2)" < data.yaml
jq -r '.[] | [.id, .name, .email] | @csv' data.json > data.csv
python3 -c "
import csv, json, sys
reader = csv.DictReader(open(sys.argv[1]))
print(json.dumps(list(reader), indent=2))
" data.csv
python3 -c "import json, tomli_w, sys; tomli_w.dump(json.load(sys.stdin), sys.stdout.buffer)" < data.json
jq '.' data.json
python3 -m json.tool data.json
Binary formats (inspection)
python3 -c "
import msgpack, json, sys
data = msgpack.unpackb(sys.stdin.buffer.read(), raw=False)
print(json.dumps(data, indent=2))
" < data.msgpack
protoc --decode_raw < data.pb
python3 -c "
import cbor2, json, sys
data = cbor2.loads(sys.stdin.buffer.read())
print(json.dumps(data, indent=2, default=str))
" < data.cbor
Quick Decode Script
#!/bin/bash
INPUT="${1:-$(cat)}"
B64_DECODED=$(echo "$INPUT" | base64 -d 2>/dev/null)
if [[ $? -eq 0 && -n "$B64_DECODED" ]]; then
echo "Base64 → $B64_DECODED"
fi
if echo "$INPUT" | grep -q '%[0-9A-Fa-f]\{2\}'; then
URL_DECODED=$(python3 -c "from urllib.parse import unquote; print(unquote('$INPUT'))" 2>/dev/null)
echo "URL → $URL_DECODED"
fi
if echo "$INPUT" | grep -qP '^eyJ[A-Za-z0-9_-]+\.eyJ[A-Za-z0-9_-]+\.'; then
echo "JWT header:"
echo "$INPUT" | cut -d. -f1 | tr '-_' '+/' | base64 -d 2>/dev/null | jq
echo "JWT payload:"
echo "$INPUT" | -d. -f2 | | -d 2>/dev/null | jq
| grep -qP && [[ $(( % )) -eq 0 ]];
HEX_DECODED=$( | xxd -r -p 2>/dev/null)
[[ -n ]];
Tips
- Base64 increases data size by ~33%. Use it for embedding binary data in text formats (JSON, XML, email), not for compression or encryption.
- Base64url (RFC 4648) uses
- and _ instead of + and /, and omits padding =. JWTs and URL parameters use this variant.
- SHA-256 is the standard for integrity checks. MD5 is fine for dedup and non-security checksums but broken for cryptographic use.
- JWTs are signed, not encrypted. Anyone can decode the header and payload. Only the signature verifies authenticity. Never put secrets in JWT claims.
- When files display garbled text (mojibake), the problem is almost always wrong encoding assumption. Check with
file -bi and re-read with the correct encoding.
xxd -p (plain hex) and xxd -r -p (reverse) are the fastest way to convert between binary and hex on the command line.
- URL-encode with
encodeURIComponent (JavaScript) or urllib.parse.quote (Python), not by hand. Manual encoding misses edge cases.