我试图将4个字节组合到一个u32中,编译器告诉我,这个变化已经过去了。这是我的密码:
pub fn get_instruction(file: &[u8], counter: usize) {
let ins = u32::from(file[counter] << 24)
| u32::from(file[counter + 1] << 16)
| u32::from(file[counter + 2] << 8)
| u32::from(file[counter + 3]);
println!("{:x}", ins);
}发布于 2019-09-19 17:57:15
你得到了你的运营商优先权并投错了:
pub fn get_instruction(file: &[u8], counter: usize) {
let ins = u32::from(file[counter]) << 24
| u32::from(file[counter + 1]) << 16
| u32::from(file[counter + 2]) << 8
| u32::from(file[counter + 3]);
println!("{:x}", ins);
}在尝试移动u8 24位后,您正在进行转换,这是您的问题所在。
发布于 2019-09-19 18:53:47
没有必要自己旋转比特--您可以使用函数u32::from_be_bytes()来代替:
pub fn get_instruction(file: &[u8], counter: usize) {
let ins_bytes = <[u8; 4]>::try_from(&file[counter..counter + 4]).unwrap();
let ins = u32::from_be_bytes(ins_bytes);
println!("{:x}", ins);
}https://stackoverflow.com/questions/58016544
复制相似问题