pub struct HexCodec { /* private fields */ }Expand description
Encodes and decodes hexadecimal byte strings.
Its low-level Codec<Value = u8, Unit = u8> implementation handles
exactly one byte as two ASCII hexadecimal units. Whole-string prefix,
per-byte prefix, separator, and whitespace handling remain part of the owned
encode and decode helpers.
Implementations§
Source§impl HexCodec
impl HexCodec
Sourcepub fn new() -> Self
pub fn new() -> Self
Creates a lowercase codec without prefix or separators.
§Returns
A hexadecimal codec using lowercase digits.
Sourcepub fn upper() -> Self
pub fn upper() -> Self
Creates an uppercase codec without prefix or separators.
§Returns
A hexadecimal codec using uppercase digits.
Sourcepub fn with_uppercase(self, uppercase: bool) -> Self
pub fn with_uppercase(self, uppercase: bool) -> Self
Sourcepub fn with_prefix(self, prefix: impl Into<String>) -> Self
pub fn with_prefix(self, prefix: impl Into<String>) -> Self
Sourcepub fn with_byte_prefix(self, prefix: impl Into<String>) -> Self
pub fn with_byte_prefix(self, prefix: impl Into<String>) -> Self
Sourcepub fn with_separator(self, separator: impl Into<String>) -> Self
pub fn with_separator(self, separator: impl Into<String>) -> Self
Sourcepub fn with_ignored_ascii_whitespace(self, ignore: bool) -> Self
pub fn with_ignored_ascii_whitespace(self, ignore: bool) -> Self
Sourcepub fn with_ignore_prefix_case(self, ignore: bool) -> Self
pub fn with_ignore_prefix_case(self, ignore: bool) -> Self
Sets whether ASCII case is ignored when decoding configured prefixes.
This option affects whole-output prefixes and per-byte prefixes during decoding only. Encoding writes prefixes exactly as configured.
§Parameters
ignore: Whether to ignore ASCII case while matching prefixes.
§Returns
The updated codec.
Sourcepub fn encode_into(&self, bytes: &[u8], output: &mut String)
pub fn encode_into(&self, bytes: &[u8], output: &mut String)
Encodes bytes into an existing string.
§Parameters
bytes: Bytes to encode.output: Destination string.
Sourcepub fn decode(&self, text: &str) -> MiscCodecResult<Vec<u8>>
pub fn decode(&self, text: &str) -> MiscCodecResult<Vec<u8>>
Decodes hexadecimal text into bytes.
§Parameters
text: Hexadecimal text.
§Returns
Decoded bytes.
§Errors
Returns MiscCodecError when a configured whole or per-byte prefix is
missing, when the normalized digit count is odd, or when a non-hex
digit is found.
Sourcepub fn decode_into(
&self,
text: &str,
output: &mut Vec<u8>,
) -> MiscCodecResult<()>
pub fn decode_into( &self, text: &str, output: &mut Vec<u8>, ) -> MiscCodecResult<()>
Decodes hexadecimal text into an existing byte vector.
§Parameters
text: Hexadecimal text.output: Destination byte vector.
§Errors
Returns MiscCodecError when the input is malformed.
Trait Implementations§
Source§impl Codec for HexCodec
impl Codec for HexCodec
Source§fn min_units_per_value(&self) -> NonZeroUsize
fn min_units_per_value(&self) -> NonZeroUsize
Returns the two hexadecimal digits needed for one byte.
Source§fn max_units_per_value(&self) -> NonZeroUsize
fn max_units_per_value(&self) -> NonZeroUsize
Returns the two hexadecimal digits needed for one byte.
Source§unsafe fn decode_unchecked(
&self,
input: &[u8],
index: usize,
) -> Result<(u8, NonZeroUsize), Self::DecodeError>
unsafe fn decode_unchecked( &self, input: &[u8], index: usize, ) -> Result<(u8, NonZeroUsize), Self::DecodeError>
Decodes one byte from two ASCII hexadecimal digits.
Source§unsafe fn encode_unchecked(
&self,
value: &u8,
output: &mut [u8],
index: usize,
) -> Result<usize, Self::EncodeError>
unsafe fn encode_unchecked( &self, value: &u8, output: &mut [u8], index: usize, ) -> Result<usize, Self::EncodeError>
Encodes one byte as two ASCII hexadecimal digits.