KokaKiwi / rust-hex

A basic crate to encode values to hexadecimal representation. Originally extracted from rustc-serialize.
https://crates.io/crates/hex
Apache License 2.0
201 stars 55 forks source link

Need a prefix version to handle hex data more easily. #51

Open yjhmelody opened 3 years ago

yjhmelody commented 3 years ago

Such as:

pub trait ToHexExt {
    /// Encode the hex strict representing `self` into the result with prefix `0x`. Lower case
    /// letters are used (e.g. `f9b4ca`)
    fn encode_hex_with_prefix<T: iter::FromIterator<char>>(&self) -> T;

    /// Encode the hex strict representing `self` into the result with prefix `0X`. Upper case
    /// letters are used (e.g. `F9B4CA`)
    fn encode_hex_upper_with_prefix<T: iter::FromIterator<char>>(&self) -> T;
}

impl <T: ToHex + AsRef<[u8]> >ToHexExt for T {
    fn encode_hex_with_prefix<U: iter::FromIterator<char>>(&self) -> U {
        encode_to_iter(HEX_CHARS_LOWER, &["0x".as_ref(), self.as_ref()].concat())
    }

    fn encode_hex_upper_with_prefix<U: iter::FromIterator<char>>(&self) -> U {
        encode_to_iter(HEX_CHARS_UPPER, &["0X".as_ref(), self.as_ref()].concat())
    }
}

But I find that maybe need a ext subdir for doing such work for all apis.

ildar commented 3 years ago

and decoding counterpart also.