Skip to main content

Hex

Struct Hex 

Source
pub struct Hex<'a>(/* private fields */);
Expand description

A wrapper type for &[u8] which implements Display by providing a hexdump

See HexDisplayExt for an easier method of constructing this type.

By default, it outputs a lower-case hexdump, but it outputs upper-case if provided with {:X} formatting option.

use hex_display::Hex;

assert_eq!(
    format!("{}", Hex::new(&[0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef])),
    "0123456789abcdef"
);
assert_eq!(
    format!("{:?}", Hex::new(&[0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef])),
    "Hex(0123456789abcdef)"
);
assert_eq!(
    format!("{:X}", Hex::new(&[0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef])),
    "0123456789ABCDEF"
);

The formatter’s width, align, and fill are applied to the entire output, the same way they would be for any other Display value:

use hex_display::Hex;
assert_eq!(format!("{:>8}", Hex::new(&[0x01, 0x23])), "    0123");

Passing the alternate form (#) switches the output to a multiline hexdump -C-style dump, with an 8-digit offset column, two groups of eight hex bytes, and an ASCII gutter (non-printable bytes shown as .):

use hex_display::Hex;

let bytes: [u8; 18] = [
    0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
    0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
    0x10, 0x11,
];
assert_eq!(
    format!("{:#}", Hex::new(&bytes)),
    "\
00000000  00 01 02 03 04 05 06 07  08 09 0a 0b 0c 0d 0e 0f  |................|
00000010  10 11                                             |..|",
);

The alternate form combines with the upper-case flag ({:#X}) and with width-padding ({:>WIDTH$#}) just like the single-line form.

If the hex dump length exceeds the width in the output format (longer than 4 GiB), then the leading digit is replaced with a *:

...
fffffff0  00 01 02 03 04 05 06 07  08 09 0a 0b 0c 0d 0e 0f  |................|
*0000000  00 01 02 03 04 05 06 07  08 09 0a 0b 0c 0d 0e 0f  |................|
*0000010  00 01 02 03 04 05 06 07  08 09 0a 0b 0c 0d 0e 0f  |................|

Implementations§

Source§

impl<'a> Hex<'a>

Source

pub fn new(s: &'a (impl AsRef<[u8]> + ?Sized)) -> Self

Create a new Hex instance for the given bytes.

Trait Implementations§

Source§

impl<'a> Clone for Hex<'a>

Source§

fn clone(&self) -> Hex<'a>

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 Debug for Hex<'_>

Source§

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

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

impl Display for Hex<'_>

Source§

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

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

impl LowerHex for Hex<'_>

Source§

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

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

impl UpperHex for Hex<'_>

Source§

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

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

impl<'a> Copy for Hex<'a>

Auto Trait Implementations§

§

impl<'a> Freeze for Hex<'a>

§

impl<'a> RefUnwindSafe for Hex<'a>

§

impl<'a> Send for Hex<'a>

§

impl<'a> Sync for Hex<'a>

§

impl<'a> Unpin for Hex<'a>

§

impl<'a> UnsafeUnpin for Hex<'a>

§

impl<'a> UnwindSafe for Hex<'a>

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> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. 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.