Convert between hex bytes, UTF-8 text and bytes32 strings for Solidity and calldata.
Loading tool
About UTF-8 Converter
Convert between hex bytes and UTF-8 text in both directions, with the formats Solidity developers actually meet: raw bytes, bytes32 (right-padded, trailing zeros stripped) and ABI-encoded string return data. When decoding, the format is auto-detected, including Error(string) revert data. When encoding, you get the raw hex, a bytes32 value when the text fits, a Solidity hex"..." literal and the ABI-encoded string words.
Common uses
- Read a bytes32 role name, token symbol or identifier as text
- Turn a short string into a bytes32 constant for a contract
- Decode a string returned by eth_call
- Read the message inside Error(string) revert data
FAQ
- How do I convert a string to bytes32 in Solidity?
- Take the UTF-8 bytes of the string and right-pad them with zeros to 32 bytes. Strings longer than 32 bytes do not fit in a bytes32.
- How is a string ABI-encoded?
- As an offset word, then a length word with the byte count, then the UTF-8 bytes right-padded with zeros to a multiple of 32 bytes.
- Why does my decoded bytes32 string have strange characters?
- Not every bytes32 holds text. Hashes and packed numbers are binary data, so decoding them as UTF-8 produces invalid or unprintable characters.