pub fn string_to_binary(txt: &str) -> Result<Vec<u32>, String>
Expand description
This function returns the binary numbers of each letter passed in.
This function takes each char
of the &str
passed in and converts it to a binary number which is represented
as a u32
. These are then pushed into a Vec<u32>
.
ยงExample
use ascii_converter::*;
let expected = vec![1001000, 1100101, 1101100, 1101100, 1101111, 100000, 1110111, 1101111, 1110010, 1101100, 1100100, 100001];
assert_eq!(string_to_binary(&"Hello world!").unwrap(), expected);
Examples found in repository?
examples/conversion.rs (line 18)
4fn main() {
5
6 let mut name = String::new();
7
8 print!("Enter name: ");
9
10 stdout().flush().expect("unable to flush buffer");
11
12 //reads user input and assigns it to the name variable
13 stdin().read_line(&mut name).unwrap();
14
15 let name = name.trim();
16
17 //outputs the binary representation
18 println!("* {} in Binary: {:?}", name, string_to_binary(&name).unwrap());
19
20 //outputs the decimal representation
21 println!("* {} in Decimal: {:?}", name, string_to_decimals(&name).unwrap());
22
23}