1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/*!
# `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
- No runtime panics (except for internal bugs)
## Encoding
```
use hexhex::hex;
let bytes = [0xc0, 0xff, 0xee];
println!("{}", hex(&bytes)); // no allocations, prints "c0ffee"
```
Or if you want more control:
```
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 [`core::fmt::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");
```
One perhaps surprising conversion is that of an empty byte slice when prefixes are enabled:
```
use hexhex::Hex;
assert_eq!(Hex::new(b"").with_prefix(true).to_string(), "0x");
```
## Decoding (no allocations)
```
use hexhex::decode_to_buf_exact;
let hex = "0xc0fFEe";
let mut buf = [0u8; 3];
assert!(decode_to_buf_exact(hex, &mut buf).is_ok());
assert_eq!(buf, [0xc0, 0xff, 0xee]);
```
There are some other variants, check the list of functions to see them all.
The `ascii` variants take byte strings (`&[u8]`) which need not contain ASCII or UTF-8 (however, only valid ASCII can be valid hex strings).
## Decoding (std)
```
#[cfg(feature = "std")]
{
use hexhex::decode;
assert_eq!(&decode("c0ffee").unwrap(), &[0xc0, 0xff, 0xee]);
}
```
```
#[cfg(feature = "std")]
{
use hexhex::decode_ascii;
assert_eq!(&decode_ascii(b"c0ffee").unwrap(), &[0xc0, 0xff, 0xee]);
}
```
## Macro
```
use hexhex::hex_literal;
let bytes: &[u8; 3] = hex_literal!("0xc0ffee");
assert_eq!(bytes, &[0xc0, 0xff, 0xee]);
```
Malformed hex strings will cause a compile-time error:
```compile_fail
use hexhex::hex_literal;
let bytes = hex_literal!("c0f"); // odd number of hex digits is invalid
```
The macro is a proc-macro, not a declarative macro; it can be used in a `match` arm:
```
use hexhex::hex_literal;
let x = &[0x12, 0x34];
match x {
hex_literal!("1234") => {},
_ => panic!(),
}
```
## Feature flags
- `std` (enabled by default): Enables functionality that makes use of `std`. With this flag disabled, the crate is `#![no_std]` compatible.
*/
pub use ;
pub use ;
pub use *;