0

I want to read some data from a file in order to xor this with another sequence. The content of the file is

00112233445566778899aabbccddeeff

The sequence this should be xored with is

000102030405060708090a0b0c0d0e0f

The result should be:

00102030405060708090a0b0c0d0e0f0

The reason why i get a differnt result is that rust reads the content as ascii, like this:

buffer: [48, 48, 49, 49, 50, 50, 51, 51, 52, 52, 53, 53, 54, 54, 55, 55]
buffer: [56, 56, 57, 57, 97, 97, 98, 98, 99, 99, 100, 100, 101, 101, 102, 102]

Is there a way to read the content directly to an hex array or how would one convert this?

Emanuel
  • 69
  • 1
  • 10
  • 1
    [Function hex::decode](https://docs.rs/hex/0.3.1/hex/fn.decode.html) appears to be appropriate. – Andrew Morton May 01 '20 at 14:19
  • 3
    Does this answer your question? [Converting a hexadecimal string to a decimal integer](https://stackoverflow.com/questions/32381414/converting-a-hexadecimal-string-to-a-decimal-integer) – phimuemue May 01 '20 at 14:48
  • @phimuemue Does Rust have an integer type large enough? The first example is 88962710306127702866241727433142015 in decimal, but the largest value for an i64 is 9223372036854775807. – Andrew Morton May 01 '20 at 16:37
  • These values represented in the buffer slices, for example 48, are dec values of the ascii. 48 corresponds 30 in hex and is 0/zero. The above mentioned sequence (content of file) is what i actually want: 00112233445566778899aabbccddeeff If i edited the file content using the function from_str_radix, i just get "30", which is not what i want. Therefore for this project i can't import another crate, so hex::decode is not possible, there must be another solution for this problem – Emanuel May 01 '20 at 16:41
  • @Emanuel Perhaps you could use the source code from `hex`. Otherwise you'll just have to parse it yourself - a search for "convert hex to bytes" will give you the gist in many languages. – Andrew Morton May 01 '20 at 18:17

2 Answers2

0

You can use hex::decode to convert hex into bytes and then use '^' symbol to do xor operation with bits to get your result.

sbnair
  • 11
  • 1
  • Can you show some example code? I tried using [Rust Playground](https://play.rust-lang.org/), but it doesn't seem to have the `hex` crate available. – Andrew Morton May 01 '20 at 17:15
  • The buffer is a [u8;16] array. Using decode i get a Vec (which can be converted by `buf_vec.get(..).unwrap()` `let mut buf_vec = decode(buffer).unwrap(); println!("hex {:x?}", buf_vec.get(..).unwrap());` The problem with this is that the original buffer was this: `buf [30, 30, 31, 31, 32, 32, 33, 33, 34, 34, 35, 35, 36, 36, 37, 37]` And now i get the following: `hex [0, 11, 22, 33, 44, 55, 66, 77]` – Emanuel May 01 '20 at 17:34
  • I would expect something like this: `[0,0,1,1,...,f,f]` which should have the same length as the original array. – Emanuel May 01 '20 at 17:38
  • I think i have to read the file into a String variable. But in order to keep the heap as low as possible i want to read the file input block by block. That's why i originally wanted to read it into a fixed size u8-array. Is it possible to limit the size of a String or to read from a file into a char array? – Emanuel May 01 '20 at 21:15
0

This does the job, reads 16 byte blocks until the end of the file, converts it to &str and converts it again into a vector of Chars:

let mut buffer = [0;16];

    while let Ok(n) = file.read(&mut buffer) {
        if n == 0 {
            break;
        }

        let s = match str::from_utf8(&buffer) {
            Ok(str) => str,
            Err(e) => panic!("Invalid UTF-8 sequence: {}", e),
        };

        let mut content = s.to_string();
        let char_vec: Vec<char> = content.chars().collect();
        println!("Chars{:?}", char_vec);
    }
Emanuel
  • 69
  • 1
  • 10