我想讀取文件的一些原始字節,所以我查看了文檔,並將一個函數放在一起,看起來應該將字節讀入一個向量並讀取從頭開始的32位整數。爲什麼File :: bytes以不同於hexdump的順序迭代字節?
fn filetobytes(name: &'static str) -> Vec<u8> {
let file = File::open(name).expect("No such file");
let filebytes: Vec<u8> = file.bytes().map(|readbyte| readbyte.unwrap()).collect();
return filebytes;
}
fn parse_int(index: usize, vector: &Vec<u8>) -> u32 {
let mut num: u32 = 0;
for i in 0..4 {
num = num << 8;
num = num | ((vector[index + i] as u32) & 0xff);
println!("Byte is {:x}", vector[index + i]);
}
return num;
}
fn main() {
let filebytes = filetobytes("diddy.h");
println!("0x{:x}", parse_int(0, &filebytes));
}
然後,我試圖前進,但很快發現我的邏輯沒有任何工作。在做了一些嗅探之後,我發現我沒有按照我預期的順序獲取字節。例如,上面的代碼(用於打印的前四個字節分別然後合成爲一個整數)產生以下輸出
Byte is 23
Byte is 64
Byte is 65
Byte is 66
0x23646566
如果我上diddy.h
一個hexdump
,我得到以下輸出。
0000000 6423 6665 6e69 2065 4944 4444 5f59 4957
0000010 5444 2048 3031 0a35 6423 6665 6e69 2065
0000020 4944 4444 5f59 4548 4749 5448 3120 3035
0000030 630a 6e6f 7473 7520 736e 6769 656e 2064
0000040 6873 726f 2074 6964 6464 5f79 6164 6174
0000050 315b 3735 3035 3b5d 000a
0000059
奇怪的是,似乎vector[0]
訪問字節1,vector[1]
訪問字節0,vector[2]
得到字節3,vector[3]
得到字節2,依此類推。
我可能做了什麼導致這種行爲,我該如何解決它?
嘗試'hexdump -C' – ildjarn
@ pipsqueaker117如果你回答自己的問題,你可以做它作爲答案,所以你可以把它標記爲已解決? – heinrich5991
是的,請刪除已編輯的部分並將其作爲回答發佈。這是[非常好](https://stackoverflow.com/help/self-answer)在stackoverflow上回答你自己的問題。 – user4815162342