3 stable releases

1.1.1 Sep 29, 2023
1.0.0 May 26, 2023

#1083 in Rust patterns

Download history 49/week @ 2024-01-04 14/week @ 2024-01-11 11/week @ 2024-01-18 20/week @ 2024-01-25 28/week @ 2024-02-01 18/week @ 2024-02-08 61/week @ 2024-02-15 45/week @ 2024-02-22 48/week @ 2024-02-29 113/week @ 2024-03-07 54/week @ 2024-03-14 67/week @ 2024-03-21 84/week @ 2024-03-28 45/week @ 2024-04-04 49/week @ 2024-04-11 31/week @ 2024-04-18

218 downloads per month
Used in 4 crates (via include_assets_decode)

EUPL-1.2

40KB
853 lines

hexhex 🪄 hexadecimal conversion

Features:

  • Display bytes as hex with no (heap) allocations
  • Convert bytes to hex String
  • Convert hex &str or &[u8] to a new byte vector
  • Convert hex &str or &[u8] to bytes in a preallocated buffer
  • Macro for all your compile-time hex to bytes conversion needs
  • #![no_std] support for a subset of the above (if used with no default features)
  • No runtime panics (except for internal bugs)

Encoding

use hexhex::Hex;
let bytes = [0xc0, 0xff, 0xee];
println!("{}", Hex::new(&bytes)); // no allocations, prints "c0ffee"
use hexhex::{Hex, Case};
let bytes = [0xc0, 0xff, 0xee];
println!("{}", Hex::new(&bytes).with_prefix(true).with_case(Case::Upper)); // no allocations, prints "0xC0FFEE"

Encode to String

Hex implements the Display trait, so conversion to string is as easy as:

use hexhex::Hex;
let bytes = [0xc0, 0xff, 0xee];
let hex = Hex::new(&bytes).to_string();
assert_eq!(hex, "c0ffee");

Decoding

Allocating, only with std:

use hexhex::decode;
assert_eq!(&decode("c0ffee").unwrap(), &[0xc0, 0xff, 0xee]);

Macro

use hexhex::hex_literal;
let bytes: &[u8; 3] = hex_literal!("0xc0ffee");
assert_eq!(bytes, &[0xc0, 0xff, 0xee]);

License

Licensed under the EUPL-1.2-or-later

Contributing

Bug reports are very much appreciated. Feel free to request features, but no promises. I do not intend to accept third-party patches at this time.

Dependencies

~87KB