Andrew Shay logo
Blog & Digital Garden
Home > Blog > Rust Convert u32 to ...

Rust Convert u32 to bytes. Convert bytes to u32.

Here is how to convert u32 to bytes and then back again in Rust.

fn main() {
    let original_u32: u32 = 1048572;
    println!("{}", original_u32);
    let u32_as_bytes: [u8; 4] = original_u32.to_be_bytes();
    println!("{:?}", u32_as_bytes);
    let back_to_u32: u32 = u32::from_be_bytes(u32_as_bytes);
    println!("{}", back_to_u32);


On GitHub