Skip to main content

HexCodec

Struct HexCodec 

Source
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

Source

pub fn new() -> Self

Creates a lowercase codec without prefix or separators.

§Returns

A hexadecimal codec using lowercase digits.

Source

pub fn upper() -> Self

Creates an uppercase codec without prefix or separators.

§Returns

A hexadecimal codec using uppercase digits.

Source

pub fn with_uppercase(self, uppercase: bool) -> Self

Sets whether encoded digits should be uppercase.

§Parameters
  • uppercase: Whether to use uppercase hexadecimal digits.
§Returns

The updated codec.

Source

pub fn with_prefix(self, prefix: impl Into<String>) -> Self

Sets a whole-output prefix.

The prefix is written once before the encoded bytes and required once before decoded input. For example, using prefix 0x encodes bytes as 0x1f8b.

§Parameters
  • prefix: Whole-output prefix text such as 0x.
§Returns

The updated codec.

Source

pub fn with_byte_prefix(self, prefix: impl Into<String>) -> Self

Sets a per-byte prefix.

The prefix is written before every encoded byte and required before every decoded byte. For example, using byte prefix 0x and separator encodes bytes as 0x1f 0x8b.

§Parameters
  • prefix: Per-byte prefix text such as 0x.
§Returns

The updated codec.

Source

pub fn with_separator(self, separator: impl Into<String>) -> Self

Sets a separator written and accepted between encoded bytes.

§Parameters
  • separator: Separator text.
§Returns

The updated codec.

Source

pub fn with_ignored_ascii_whitespace(self, ignore: bool) -> Self

Sets whether ASCII whitespace is ignored while decoding.

§Parameters
  • ignore: Whether to ignore ASCII whitespace.
§Returns

The updated codec.

Source

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.

Source

pub fn encode(&self, bytes: &[u8]) -> String

Encodes bytes into a hexadecimal string.

§Parameters
  • bytes: Bytes to encode.
§Returns

Hexadecimal text.

Source

pub fn encode_into(&self, bytes: &[u8], output: &mut String)

Encodes bytes into an existing string.

§Parameters
  • bytes: Bytes to encode.
  • output: Destination string.
Source

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.

Source

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 Clone for HexCodec

Source§

fn clone(&self) -> HexCodec

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Codec for HexCodec

Source§

fn min_units_per_value(&self) -> NonZeroUsize

Returns the two hexadecimal digits needed for one byte.

Source§

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>

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>

Encodes one byte as two ASCII hexadecimal digits.

Source§

type Value = u8

Logical value decoded from or encoded into the buffer.
Source§

type Unit = u8

Buffer unit used by the encoded representation.
Source§

type DecodeError = MiscCodecError

Error reported when decoding malformed units.
Source§

type EncodeError = MiscCodecError

Error reported when encoding an unsupported value.
Source§

impl Debug for HexCodec

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for HexCodec

Source§

fn default() -> Self

Creates a lowercase codec without prefix or separators.

Source§

impl Eq for HexCodec

Source§

impl PartialEq for HexCodec

Source§

fn eq(&self, other: &HexCodec) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl StructuralPartialEq for HexCodec

Source§

impl ValueDecoder<str> for HexCodec

Source§

fn decode(&self, input: &str) -> Result<Self::Output, Self::Error>

Decodes hexadecimal text into bytes.

Source§

type Error = MiscCodecError

Decoding error type.
Source§

type Output = Vec<u8>

Decoded output type.
Source§

impl ValueEncoder<[u8]> for HexCodec

Source§

fn encode(&self, input: &[u8]) -> Result<Self::Output, Self::Error>

Encodes bytes into hexadecimal text.

Source§

type Error = MiscCodecError

Encoding error type.
Source§

type Output = String

Encoded output type.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.