Skip to content
This repository has been archived by the owner on Oct 7, 2023. It is now read-only.

Create BO87AN.rs #1643

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions f616xP/BO87AN.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
use std::io::Write;

fn read_string() -> String
{
let mut text = String::new();
std::io::stdin().read_line(&mut text)
.expect("Error while getting input");
return text;
}

fn exit()
{
println!("Press enter to exit...");
read_string();
}

fn main()
{
println!("\n");
let mut dec: u64 = 0;
print!("Enter a binary number: ");
std::io::stdout().flush().unwrap();
let bin = read_string();
for (i, bit) in bin.trim().chars().rev().enumerate()
{
let bit_num = bit.to_digit(10)
.expect("Error while parsing bit");
dec += (bit_num * u32::pow(2, i as u32)) as u64;
}
println!("Binary: {}, Decimal: {}", bin.trim(), dec);
exit();
}