Try it Live
Run RLP examples in the interactive playground
String Encoding Rules
RLP string encoding has three cases based on byte length:1. Single Byte < 0x80
For a single byte with value less than 0x80 (128), the byte encodes as itself with no prefix:2. Short String (0-55 bytes)
For strings of 0-55 bytes, prefix with0x80 + length:
3. Long String (56+ bytes)
For strings of 56+ bytes, use long form:[0xb7 + length_of_length, ...length_bytes, ...bytes]
The threshold of 56 bytes is chosen because lengths 0-55 fit in a single prefix byte (0x80-0xb7). Starting at 56, we need an additional byte to encode the length.
Usage Patterns
Transaction Field Encoding
Encode individual transaction fields:Contract Bytecode
Encode contract bytecode (often > 56 bytes):Keccak256 Hash
Encode 32-byte hash values:Empty Data
Encode empty byte arrays:Performance
When to Use encodeBytes
UseencodeBytes instead of generic encode when you know the input is bytes (not a list), to skip type dispatch overhead.
Pre-calculate Sizes
For repeated encoding, pre-calculate buffer sizes:Batch Encoding
Encode multiple byte arrays efficiently:See Also
- encode - Universal encoder
- encodeList - Encode list
- encodeArray - Encode with schema
- decode - Decode RLP bytes
- Algorithm - RLP specification

