Expand description

Encoding and decoding of the Bech32 format

Bech32 is an encoding scheme that is easy to use for humans and efficient to encode in QR codes.

A Bech32 string consists of a human-readable part (HRP), a separator (the character '1'), and a data part. A checksum at the end of the string provides error detection to prevent mistakes when the string is written off or read out loud.

The original description in BIP-0173 has more details.


use bech32::{self, FromBase32, ToBase32, Variant};
let encoded = bech32::encode("bech32", vec![0x00, 0x01, 0x02].to_base32(), Variant::Bech32).unwrap();
assert_eq!(encoded, "bech321qqqsyrhqy2a".to_string());
let (hrp, data, variant) = bech32::decode(&encoded).unwrap();
assert_eq!(hrp, "bech32");
assert_eq!(Vec::<u8>::from_base32(&data).unwrap(), vec![0x00, 0x01, 0x02]);
assert_eq!(variant, Variant::Bech32);


Allocationless Bech32 writer that accumulates the checksum data internally and writes them out in the end.

Integer in the range 0..32


Error types for Bech32 encoding / decoding

Used for encode/decode operations for the two variants of Bech32


Interface to calculate the length of the base32 representation before actually serializing

A trait to convert between u8 arrays and u5 arrays without changing the content of the elements, but checking that they are in range.

Parse/convert base32 slice to Self. It is the reciprocal of ToBase32.

A trait for converting a value to a type T that represents a u5 slice.

Interface to write u5s into a sink


Convert between bit sizes

Decode a bech32 string into the raw HRP and the data bytes.

Encode a bech32 payload to string.

Encode a bech32 payload to an fmt::Write. This method is intended for implementing traits from std::fmt.