Crate hextool

Source
Expand description

This crate provides functions that allows you to easily convert strings from and to hex.

§Usage

This crate is on crates.io and can be added to your project.

[dependencies]
hextool = { version = "version", default-features = false }

§Examples

§Example: converting string to hex

To convert a string to hex, use the Hex and Convert trait:

use hextool::{Hex, Convert};

// Convert a string to hex
let hex = Hex::convert("hello", false, false);
println!("hello in hex: {}", hex); // #=> "hello in hex: 68656c6c6f"

// You can also split the output in bytes
let hex = Hex::convert("hello", false, true);
println!("hello in hex: {}", hex); // #=> "hello in hex: 68 65 6c 6c 6f"

// Convert a string with numeric flag. This will take the numerical value of the string.
// If the string is not a number, it will return an error.
let hex = Hex::convert("255", true, false);
println!("255 in hex: {}", hex); // #=> "255 in hex: {ff}"

§Example: converting hex to string

To convert a hex string to a string, use the UnHex and Convert trait:

use hextool::{UnHex, Convert};

// Convert a hex string to a string
let unhex = UnHex::convert("68656c6c6f", false, false);
println!("68656c6c6f in string: {}", unhex); // #=> "68656c6c6f in string: hello"

// Convert a hex string to a string with numeric flag. This will take the numerical value of the string.
let unhex = UnHex::convert("cafebabe", true, false);
println!("cafebabe in string: {}", unhex); // #=> "cafebabe in string: 3405691582"

// If numeric is set to false, only valid strings [0-9a-f] is accepted. If the string is not valid,
// it will return the string with the invalid string highlighted to red.
let unhex = UnHex::convert("aga", true, false);
println!("{}", unhex); // #=> "The highlighted chars can't be converted:\nag\u{1b}[31ma\u{1b}[0m."

Structs§

Hex
Hex struct
HexToolError
Error type for hextool
UnHex
UnHex struct

Traits§

Convert
Convert trait