0

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!

0

1 Answer 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();
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.