Skip to content

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

@yjhmelody

Description

@yjhmelody

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions