Module std::encoding::hex

Hexadecimal encoding and decoding.

Encode binary data to hexadecimal strings (lowercase or uppercase) and decode hexadecimal strings back to binary data.

Examples

import std::encoding::hex;

fn main() {
    let data: bytes = bytes::new();
    data.push(0xDE);
    data.push(0xAD);
    let s = hex.encode(data);
    println(s);   // "dead"
    let back = hex.decode(s);
    println(back.len());  // 2
}

Contents

Functions

Function encode

pub fn encode(data: bytes) -> String

Encode binary data to a lowercase hex string.

Function encode_upper

pub fn encode_upper(data: bytes) -> String

Encode binary data to an uppercase hex string.

Function decode

pub fn decode(s: String) -> bytes

Decode a hex string to binary data.

Returns an empty bytes buffer if the input is not valid hex.

Function encode_impl

fn encode_impl(data: bytes, upper: bool) -> String

Encode binary data to a hex string, optionally uppercase.

Function nibble_to_char

fn nibble_to_char(n: i32, upper: bool) -> i32

Convert a 4-bit nibble (0โ€“15) to its ASCII hex character code.

Function hex_char_to_nibble

fn hex_char_to_nibble(ch: String) -> i32

Convert a single hex character to its 4-bit nibble value, or -1 if invalid.