added argument to change integer base

This commit is contained in:
ika4422 2026-04-08 21:41:26 +01:00
parent f9a479f4b5
commit 96bacd47bf
No known key found for this signature in database
GPG key ID: F7FBF981D2A33785
2 changed files with 24 additions and 4 deletions

View file

@ -48,6 +48,10 @@ struct Args {
/// Specifies how many codes to generate
count: usize,
#[arg(short, long, default_value_t = 10)]
/// Input/output base for integers (2, 8, 10, 16)
base: u32,
#[arg(long, default_value_t = false)]
/// Will generate codes without encoding it into dollcode
no_doll: bool,
@ -88,7 +92,7 @@ fn main() {
let as_num = if args.text {
None
} else {
BigUint::parse_bytes(generated.as_bytes(), 10)
BigUint::parse_bytes(generated.as_bytes(), args.base)
};
match as_num {
@ -106,7 +110,8 @@ fn main() {
}
}
} else if let Some(input) = args.encode {
let encode_as_text = args.text || input.parse::<BigUint>().is_err();
let encode_as_text =
args.text || BigUint::parse_bytes(input.as_bytes(), args.base).is_none();
if encode_as_text {
let result: Vec<String> = input
@ -115,7 +120,7 @@ fn main() {
.collect();
println!("{}", result.join(" "));
} else {
let num = input.parse::<BigUint>().unwrap();
let num = BigUint::parse_bytes(input.as_bytes(), args.base).unwrap();
match to_dollcode(num) {
Ok(code) => println!("{}", code),
@ -149,7 +154,7 @@ fn main() {
println!("{}", decoded_chars);
} else {
match from_dollcode(&input) {
Ok(val) => println!("{}", val),
Ok(val) => println!("{}", val.to_str_radix(args.base)),
Err(e) => {
eprintln!("Error: {}", e);
process::exit(1);