Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 803
the_endian Avatar asked Nov 15 '18 06:11

the_endian


People also ask

How many bytes is a U8?

Sizeof(U8) is 1 - 1 byte (8 bit).

What is U8 type in Rust?

u8 : The 8-bit unsigned integer type. u16 : The 16-bit unsigned integer type. u32 : The 32-bit unsigned integer type. u64 : The 64-bit unsigned integer type. usize : The pointer-sized unsigned integer type.


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
}
like image 169
hellow Avatar answered Oct 13 '22 20:10

hellow


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.

like image 41
Benjamin Lindley Avatar answered Oct 13 '22 20:10

Benjamin Lindley