pub struct HexString<const C: Case>(/* private fields */);
Expand description
Provides a structured representation of a hexadecimal string.
It is guaranteed to be a valid hexadecimal string, whether initialized from a string or from bytes. A valid [’HexString`] should contain only alphanumerical characters such as :
- ff04ad992c
- FF04AD99C
And must not mix upper and lower alphabetic characters.
§Examples
The idiomatic way to construct a HexString
is to call HexString::new
method with a
string.
use hexstring::{HexString, Case};
let hex = HexString::<{ Case::Upper }>::new("ABCDEF").unwrap();
As the example shown, creating a hexadecimal string is a bit convoluted due to the usage of
const generic parameter.
Two convenient type aliases must be used instead of the raw HexString
type :
use hexstring::{UpperHexString, LowerHexString};
let lowercase_hex = LowerHexString::new("abcdef").unwrap();
let uppercase_hex = UpperHexString::new("ABCDEF").unwrap();
HexString
has support for conversion from and into array of bytes.
use hexstring::LowerHexString;
let expected_bytes = [41, 24, 42];
let hex = LowerHexString::from(expected_bytes);
let bytes = Vec::from(hex);
assert_eq!(expected_bytes, &bytes[..]);
Implementations§
Source§impl<const C: Case> HexString<C>
impl<const C: Case> HexString<C>
Source§impl HexString<{ Case::Lower }>
impl HexString<{ Case::Lower }>
Sourcepub fn to_uppercase(self) -> UpperHexString
pub fn to_uppercase(self) -> UpperHexString
Constructs an UpperHexString
from a LowerHexString
.
This method performs a copy if the internal string is a string literal.
Source§impl HexString<{ Case::Upper }>
impl HexString<{ Case::Upper }>
Sourcepub fn to_lowercase(self) -> LowerHexString
pub fn to_lowercase(self) -> LowerHexString
Constructs a LowerHexString
from an UpperHexString
.
This method performs a copy if the internal string is a string literal.