std-method

Integer big-endian bytes

Encode a fixed-width field with its most significant byte first, as required by many network formats. Expected stdout (not executed): [1, 2, 3, 4] true Common mistake: Do not assume all protocols use big-endian order.

Formal Syntax & Structure

rust syntaxrust
u32::to_be_bytes(self) -> [u8; 4]; u32::from_be_bytes([u8; 4]) -> u32

Working Example

Example Snippetrust
fn main() {    let bytes = 0x0102_0304_u32.to_be_bytes();    println!("{:?}", bytes);    println!("{}", u32::from_be_bytes(bytes) == 0x0102_0304);}
Rust Reference