'Converting binary string to binary [duplicate]

I have binary strings:

let x = "0b101101111";
let y = "0b111111";

How do I convert the string to binary to be able to find the sum?

this works:

let x = 0b101101111;
let y = 0b111111;
println!("x + y = {:b} ", x + y);
// x + y = 110101110 

thanks!



Solution 1:[1]

If you know that the string always begins with the constant 0b characters, then you can simply strip those and read the actual digits using from_str_radix.

let x = "0b101101111";
let x_num = i64::from_str_radix(x[2..], 2).unwrap();

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Silvio Mayolo