-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.rs
More file actions
26 lines (22 loc) · 710 Bytes
/
main.rs
File metadata and controls
26 lines (22 loc) · 710 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
use std::io::{self, Write};
fn main() {
let mut input = String::new();
print!("Enter a decimal number or text: ");
io::stdout().flush().unwrap();
io::stdin().read_line(&mut input).expect("Failed to read line");
let binary_num = match input.trim().parse::<u64>() {
Ok(num) => format!("{:b}", num),
Err(_) => {
let mut binary_num = String::new();
for byte in input.trim().as_bytes() {
binary_num.push_str(&format!("{:08b}", byte));
}
binary_num
}
};
if binary_num.is_empty() {
println!("Invalid input");
} else {
println!("Binary representation: {}", binary_num);
}
}