Expand description
Encoding utilities for hex, base58, base64, and UTF-8.
This module provides functions for encoding and decoding data in various formats commonly used in Bitcoin and blockchain applications.
§Examples
§Hex encoding
use bsv_rs::primitives::encoding::{to_hex, from_hex};
let bytes = vec![0xde, 0xad, 0xbe, 0xef];
let hex_str = to_hex(&bytes);
assert_eq!(hex_str, "deadbeef");
let decoded = from_hex("DEADBEEF").unwrap();
assert_eq!(decoded, bytes);§Base58 encoding
use bsv_rs::primitives::encoding::{to_base58, from_base58};
// Leading zeros become '1' characters
let bytes = vec![0x00, 0x00, 0x00];
let encoded = to_base58(&bytes);
assert_eq!(encoded, "111");
let decoded = from_base58("111").unwrap();
assert_eq!(decoded, bytes);§Reader and Writer
use bsv_rs::primitives::encoding::{Reader, Writer};
// Writing binary data
let mut writer = Writer::new();
writer.write_u8(0x01);
writer.write_u32_le(0x12345678);
writer.write_var_int(1000);
writer.write_var_bytes(b"hello");
let data = writer.into_bytes();
// Reading it back
let mut reader = Reader::new(&data);
assert_eq!(reader.read_u8().unwrap(), 0x01);
assert_eq!(reader.read_u32_le().unwrap(), 0x12345678);
assert_eq!(reader.read_var_int().unwrap(), 1000);
assert_eq!(reader.read_var_bytes().unwrap(), b"hello");
assert!(reader.is_empty());Structs§
- Reader
- A binary reader for parsing Bitcoin data structures.
- Writer
- A binary writer for serializing Bitcoin data structures.
Constants§
- BASE58_
ALPHABET - The Bitcoin Base58 alphabet. Excludes 0, O, I, and l to avoid ambiguity.
Functions§
- bounded_
capacity - Computes a safe pre-allocation capacity for a collection whose element count was read from untrusted input.
- from_
base58 - Decodes a Base58 string to bytes using the Bitcoin alphabet.
- from_
base64 - Decodes a Base64 string to bytes.
- from_
base58_ check - Decodes a Base58Check encoded string.
- from_
base58_ check_ with_ prefix_ length - Decodes a Base58Check encoded string with a custom prefix length.
- from_
hex - Decodes a hexadecimal string to bytes.
- from_
utf8_ bytes - Converts UTF-8 bytes to a string.
- to_
base58 - Converts a byte slice to a Base58 string using the Bitcoin alphabet.
- to_
base64 - Encodes bytes to a Base64 string.
- to_
base58_ check - Encodes data with a version prefix using Base58Check encoding.
- to_hex
- Converts a byte slice to a lowercase hexadecimal string.
- to_
utf8_ bytes - Converts a string to its UTF-8 byte representation.