0

i have an integer like this:

let x: u8 = 0b101;

and i need to convert it to string like this:

let s = "101";

or array:

let a = [1, 0, 1];

but rust by default converting already converted binary int to decimal to string so i receiving:

let x = 0b101;
println!("{:?}", x.to_string()); // 5

how to convert binary number to string or array without this?

i tried x.to_ne_bytes() and to_be_bytes() but it`s also returning alredy converted do dec number

1 Answers1

0

Simply use binary format:

let x = 0b101;
let s = format!("{:b}", x);
Stargateur
  • 24,473
  • 8
  • 65
  • 91