mirror of
https://github.com/Noratrieb/NumberSystemConverter.git
synced 2026-01-14 15:55:04 +01:00
Compare commits
1 commit
| Author | SHA1 | Date | |
|---|---|---|---|
| 70810caa6e |
1 changed files with 108 additions and 67 deletions
175
src/converter.rs
175
src/converter.rs
|
|
@ -2,7 +2,7 @@ pub fn convert(from: &str, to: &str, val: &str) -> Result<String, String> {
|
||||||
let int_value = match from {
|
let int_value = match from {
|
||||||
"b" => from_bin_to_int(val)?,
|
"b" => from_bin_to_int(val)?,
|
||||||
"o" => from_oct_to_int(val)?,
|
"o" => from_oct_to_int(val)?,
|
||||||
"d" => from_dec_to_int(val),
|
"d" => from_dec_to_int(val)?,
|
||||||
"h" => from_hex_to_int(val)?,
|
"h" => from_hex_to_int(val)?,
|
||||||
_ => return Err(format!("Not a valid system: {}", from))
|
_ => return Err(format!("Not a valid system: {}", from))
|
||||||
};
|
};
|
||||||
|
|
@ -12,76 +12,49 @@ pub fn convert(from: &str, to: &str, val: &str) -> Result<String, String> {
|
||||||
"o" => Ok(from_int_to_oct(int_value)),
|
"o" => Ok(from_int_to_oct(int_value)),
|
||||||
"d" => Ok(from_int_to_dec(int_value)),
|
"d" => Ok(from_int_to_dec(int_value)),
|
||||||
"h" => Ok(from_int_to_hex(int_value)),
|
"h" => Ok(from_int_to_hex(int_value)),
|
||||||
_ =>Err(format!("Not a valid system: {}", from))
|
_ => Err(format!("Not a valid system: {}", from))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn from_int_to_oct(mut val: i32) -> String {
|
//to int
|
||||||
let mut s = String::new();
|
|
||||||
if val == 0 {
|
|
||||||
s += "0";
|
|
||||||
}
|
|
||||||
while val > 0 {
|
|
||||||
let rest = val % 8;
|
|
||||||
s += &rest.to_string();
|
|
||||||
val = val / 8;
|
|
||||||
}
|
|
||||||
|
|
||||||
return s.chars().rev().collect();
|
pub fn from_dec_to_int(val: &str) -> Result<u128, String> {
|
||||||
}
|
|
||||||
|
|
||||||
pub fn from_int_to_hex(mut val: i32) -> String {
|
|
||||||
let mut s = String::new();
|
|
||||||
if val == 0 {
|
|
||||||
s += "0";
|
|
||||||
}
|
|
||||||
while val > 0 {
|
|
||||||
let rest = val % 16;
|
|
||||||
let to_string = rest.to_string();
|
|
||||||
|
|
||||||
s += match rest {
|
|
||||||
10 => "A",
|
|
||||||
11 => "B",
|
|
||||||
12 => "C",
|
|
||||||
13 => "D",
|
|
||||||
14 => "E",
|
|
||||||
15 => "F",
|
|
||||||
_ => &to_string
|
|
||||||
};
|
|
||||||
val = val / 16;
|
|
||||||
}
|
|
||||||
|
|
||||||
return s.chars().rev().collect();
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn from_int_to_bin(mut val: i32) -> String {
|
|
||||||
let mut s = String::new();
|
|
||||||
if val == 0 {
|
|
||||||
s += "0";
|
|
||||||
}
|
|
||||||
while val > 0 {
|
|
||||||
let rest = val % 2;
|
|
||||||
s += &rest.to_string();
|
|
||||||
val = val / 2;
|
|
||||||
}
|
|
||||||
|
|
||||||
return s.chars().rev().collect();
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn from_int_to_dec(val: i32) -> String {
|
|
||||||
val.to_string()
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn from_dec_to_int(val: &str) -> i32 {
|
|
||||||
val.parse().expect("Not a valid decimal number")
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn from_oct_to_int(val: &str) -> Result<i32, String> {
|
|
||||||
let mut dec = 0;
|
let mut dec = 0;
|
||||||
|
|
||||||
for (i, c) in val.chars().rev().enumerate() {
|
for (i, c) in val.chars().rev().enumerate() {
|
||||||
let exp = i32::pow(8, i as u32);
|
|
||||||
|
|
||||||
|
if dec > (u128::MAX / 10) {
|
||||||
|
return Err(format!("Number too big: {}", val));
|
||||||
|
}
|
||||||
|
|
||||||
|
let exp = u128::pow(10, i as u32);
|
||||||
|
dec += exp * match c.to_ascii_lowercase() {
|
||||||
|
'0' => 0,
|
||||||
|
'1' => 1,
|
||||||
|
'2' => 2,
|
||||||
|
'3' => 3,
|
||||||
|
'4' => 4,
|
||||||
|
'5' => 5,
|
||||||
|
'6' => 6,
|
||||||
|
'7' => 7,
|
||||||
|
'8' => 8,
|
||||||
|
'9' => 9,
|
||||||
|
_ => return Err(format!("Not a valid decimal number: {} at char {}: {}", val, i, c))
|
||||||
|
};
|
||||||
|
}
|
||||||
|
return Ok(dec);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn from_oct_to_int(val: &str) -> Result<u128, String> {
|
||||||
|
let mut dec = 0;
|
||||||
|
|
||||||
|
for (i, c) in val.chars().rev().enumerate() {
|
||||||
|
|
||||||
|
if dec > (u128::MAX / 8) {
|
||||||
|
return Err(format!("Number too big: {}", val));
|
||||||
|
}
|
||||||
|
|
||||||
|
let exp = u128::pow(8, i as u32);
|
||||||
dec += exp * match c.to_ascii_lowercase() {
|
dec += exp * match c.to_ascii_lowercase() {
|
||||||
'0' => 0,
|
'0' => 0,
|
||||||
'1' => 1,
|
'1' => 1,
|
||||||
|
|
@ -97,12 +70,16 @@ pub fn from_oct_to_int(val: &str) -> Result<i32, String> {
|
||||||
return Ok(dec);
|
return Ok(dec);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn from_hex_to_int(val: &str) -> Result<i32, String> {
|
pub fn from_hex_to_int(val: &str) -> Result<u128, String> {
|
||||||
let mut dec = 0;
|
let mut dec = 0;
|
||||||
|
|
||||||
for (i, c) in val.chars().rev().enumerate() {
|
for (i, c) in val.chars().rev().enumerate() {
|
||||||
let exp = i32::pow(16, i as u32);
|
|
||||||
|
|
||||||
|
if dec > (u128::MAX / 16) {
|
||||||
|
return Err(format!("Number too big: {}", val));
|
||||||
|
}
|
||||||
|
|
||||||
|
let exp = u128::pow(16, i as u32);
|
||||||
dec += exp * match c.to_ascii_lowercase() {
|
dec += exp * match c.to_ascii_lowercase() {
|
||||||
'0' => 0,
|
'0' => 0,
|
||||||
'1' => 1,
|
'1' => 1,
|
||||||
|
|
@ -126,11 +103,16 @@ pub fn from_hex_to_int(val: &str) -> Result<i32, String> {
|
||||||
return Ok(dec);
|
return Ok(dec);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn from_bin_to_int(val: &str) -> Result<i32, String> {
|
pub fn from_bin_to_int(val: &str) -> Result<u128, String> {
|
||||||
let mut dec = 0;
|
let mut dec = 0;
|
||||||
|
|
||||||
for (i, c) in val.chars().rev().enumerate() {
|
for (i, c) in val.chars().rev().enumerate() {
|
||||||
let exp = i32::pow(2, i as u32);
|
|
||||||
|
if dec > (u128::MAX / 2) {
|
||||||
|
return Err(format!("Number too big: {}", val));
|
||||||
|
}
|
||||||
|
|
||||||
|
let exp = u128::pow(2, i as u32);
|
||||||
if c == '1' {
|
if c == '1' {
|
||||||
dec += exp;
|
dec += exp;
|
||||||
} else if c == '0' {} else {
|
} else if c == '0' {} else {
|
||||||
|
|
@ -140,3 +122,62 @@ pub fn from_bin_to_int(val: &str) -> Result<i32, String> {
|
||||||
|
|
||||||
return Ok(dec);
|
return Ok(dec);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//FROM INT
|
||||||
|
|
||||||
|
pub fn from_int_to_oct(mut val: u128) -> String {
|
||||||
|
let mut s = String::new();
|
||||||
|
if val == 0 {
|
||||||
|
s += "0";
|
||||||
|
}
|
||||||
|
while val > 0 {
|
||||||
|
let rest = val % 8;
|
||||||
|
s += &rest.to_string();
|
||||||
|
val = val / 8;
|
||||||
|
}
|
||||||
|
|
||||||
|
return s.chars().rev().collect();
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn from_int_to_hex(mut val: u128) -> String {
|
||||||
|
let mut s = String::new();
|
||||||
|
if val == 0 {
|
||||||
|
s += "0";
|
||||||
|
}
|
||||||
|
while val > 0 {
|
||||||
|
let rest = val % 16;
|
||||||
|
let to_string = rest.to_string();
|
||||||
|
|
||||||
|
s += match rest {
|
||||||
|
10 => "A",
|
||||||
|
11 => "B",
|
||||||
|
12 => "C",
|
||||||
|
13 => "D",
|
||||||
|
14 => "E",
|
||||||
|
15 => "F",
|
||||||
|
_ => &to_string
|
||||||
|
};
|
||||||
|
val = val / 16;
|
||||||
|
}
|
||||||
|
|
||||||
|
return s.chars().rev().collect();
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn from_int_to_bin(mut val: u128) -> String {
|
||||||
|
let mut s = String::new();
|
||||||
|
if val == 0 {
|
||||||
|
s += "0";
|
||||||
|
}
|
||||||
|
while val > 0 {
|
||||||
|
let rest = val % 2;
|
||||||
|
s += &rest.to_string();
|
||||||
|
val = val / 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
return s.chars().rev().collect();
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn from_int_to_dec(val: u128) -> String {
|
||||||
|
val.to_string()
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue