Skip to main content

codec_hex/
encode.rs

1// SPDX-FileCopyrightText: Copyright © 2026 ReallyMe LLC. All rights reserved
2//
3// SPDX-License-Identifier: Apache-2.0
4
5const LOWER_HEX: &[u8; 16] = b"0123456789abcdef";
6
7/// Encode bytes as canonical lowercase hexadecimal.
8pub fn bytes_to_lower_hex(bytes: &[u8]) -> String {
9    let mut output = String::new();
10    write_lower_hex(bytes, &mut output);
11    output
12}
13
14/// Append canonical lowercase hexadecimal to an existing string buffer.
15pub fn write_lower_hex(bytes: &[u8], output: &mut String) {
16    for byte in bytes {
17        let high = usize::from(byte >> 4);
18        let low = usize::from(byte & 0x0f);
19        output.push(char::from(LOWER_HEX[high]));
20        output.push(char::from(LOWER_HEX[low]));
21    }
22}