How can I convert the lower/upper 8 bits of a u16 to a u8 in Rust?

Viewed 10812

I want to convert a u16 to two separate u8s. I tried to use some bit masks:

use std::convert::From;

fn main() {
    let n1: u8 = 0x41;
    let n2: u16 = 0x4157;

    println!("Number:{}", char::from(n1));

    let b1: u8 = n2 & 0xFF;
    let b2: u8 = n2 >> 8;

    println!("b1: {}", b1);
    println!("b2: {}", b2);
}
error[E0308]: mismatched types
 --> src/main.rs:9:18
  |
9 |     let b1: u8 = n2 & 0xFF;
  |                  ^^^^^^^^^ expected u8, found u16

error[E0308]: mismatched types
  --> src/main.rs:10:18
   |
10 |     let b2: u8 = n2 >> 8;
   |                  ^^^^^^^ expected u8, found u16

This question is not why does the compiler raise a mismatched type error?, rather, it is How can I convert the lower/upper 8 bits of a u16 to a u8 in Rust?. Potentially, there are other ways to do this and this question does not constrain the answer to the as keyword.

2 Answers

Update: As of Rust 1.32.0 there is u16::to_be_bytes, which can be used in favor a custom function.

fn main() {
    let bytes = 28923u16.to_be_bytes();
    assert_eq!([0x70, 0xFB], bytes);
}

You can use the as keyword to convert a u16 to u8 in a safe way.

fn convert_u16_to_two_u8s_be(integer: u16) -> [u8; 2] {
    [(integer >> 8) as u8, integer as u8]
}

If you need more types or different endianness use the byteorder crate.

extern crate byteorder;

use byteorder::{WriteBytesExt, BigEndian};

fn convert_u16_to_two_u8s_be(integer: u16) -> Vec<u8> {
    let mut res = vec![];
    res.write_u16::<BigEndian>(integer).unwrap();
    res
}

You can cast between integer types with as.

let b1 = n2 as u8;
let b2 = (n2 >> 8) as u8;

Note that the masking is unnecessary, because the cast will truncate the upper bits.

Related