【发布时间】:2021-04-16 18:49:09
【问题描述】:
也许问题应该是“为什么我希望 to_be_bytes() 和 to_le_bytes() 在这里做相反的事情?”,但无论如何,情况如下:
fn main() {
let buf: [u8; 2048] // Buffer I use to receive UDP messages
let (_, entry, _) = unsafe { buf[14..].align_to::<u16>() };
// In memory entry looks like: 00 44 16 15 21 04 03 5a 03 67 03 6a 03 64 88 91
let [first, second] = entry[0].to_be_bytes();
assert_eq!(first, 0x44);
assert_eq!(second, 0x00);
}
不应该是相反的吗?大字节序不是像在内存中保留顺序一样吗?
【问题讨论】:
-
to_be_bytes()在小端系统上将交换字节
标签: rust endianness bytebuffer