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
203 stars 57 forks source link

feature request: endianness #12

Closed subversive-owl closed 6 years ago

subversive-owl commented 7 years ago

it's relatively minor, but the endianness of the output of .to_hex() may differ from the output of POSIX hexdump:

hexdump:

0000000 7c21 6c1f 6b2c 274c 9ec3 55b4 48df 86fd 
0000010 eddb 6191 73f4 b073 1ec9 2d12 314c a80b 
0000020 

to_hex: "217c1f6c2c6b4c27c39eb455df48fd86dbed9161f47373b0c91e122d4c310ba8"

Being able to choose would be a nice feature.

subversive-owl commented 7 years ago

terrible implementation to be constructive:


fn reverse_endianness(in_str: &str) -> String {
    let mut rev_str = "".to_string();
    for chunk in in_str.chars().collect::<Vec<char>>().chunks(4) {
        // hardcoding this because, ugh, seriously
        let swapped_chars: Vec<char> = vec![chunk[2],
                                            chunk[3],
                                            chunk[0],
                                            chunk[1]];
        rev_str.push_str(String::from_iter(swapped_chars).as_str());
    }
    rev_str
}```
KokaKiwi commented 6 years ago

Closing this issue as hex is only meant to convert bytes into its hexadecimal form, while handling endianness is only needed when handling integers... Plus it would require to handle different integer sizes, which would only lead to overcomplex code for the library's scope.

As an alternative, you can actually manipulate the bytes directly to reverse stuff if you really need to.

(I should have closed it sooner, sorry...)