Crate hex_literal[][src]

Expand description

This crate provides the hex! macro for converting hexadecimal string literals to a byte array at compile time.

It accepts the following characters in the input string:

  • '0'...'9', 'a'...'f', 'A'...'F' — hex characters which will be used in construction of the output byte array
  • ' ', '\r', '\n', '\t' — formatting characters which will be ignored

Additionally it accepts line (//) and block (/* .. */) comments. Characters inside of those are ignored.

Examples

// the macro can be used in const context
const DATA: [u8; 4] = hex!("01020304");
assert_eq!(DATA, [1, 2, 3, 4]);

// it understands both upper and lower hex values
assert_eq!(hex!("a1 b2 c3 d4"), [0xA1, 0xB2, 0xC3, 0xD4]);
assert_eq!(hex!("E5 E6 90 92"), [0xE5, 0xE6, 0x90, 0x92]);
assert_eq!(hex!("0a0B 0C0d"), [10, 11, 12, 13]);
let bytes = hex!("
    00010203 04050607
    08090a0b 0c0d0e0f
");
assert_eq!(bytes, [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]);

// it's possible to use several literals (results will be concatenated)
let bytes2 = hex!(
    "00010203 04050607" // first half
    "08090a0b 0c0d0e0f" // second hald
);
assert_eq!(bytes2, bytes);

// comments can be also included inside literals
assert_eq!(hex!("0a0B // 0c0d line comments"), [10, 11]);
assert_eq!(hex!("0a0B // line comments
                 0c0d"), [10, 11, 12, 13]);
assert_eq!(hex!("0a0B /* block comments */ 0c0d"), [10, 11, 12, 13]);
assert_eq!(hex!("0a0B /* multi-line
                         block comments
                      */ 0c0d"), [10, 11, 12, 13]);

Macros

Macro for converting sequence of string literals containing hex-encoded data into an array of bytes.