Add comment and refactor a bit

master
Benoît 2020-04-13 19:02:57 +02:00
parent 1f93e4531f
commit ddf6b3285c
1 changed files with 8 additions and 3 deletions

View File

@ -103,17 +103,18 @@ fn main() {
String::from("42")];
assert_eq!(hex_to_string(&test), "AB");
// Get command line argument
let args: Vec<String> = env::args().collect();
let input = &args[1];
let char_vec: Vec<char> = input.chars().collect();
// Convert Vec<char> to Vec<String> with 2 hexadecimal chars
// ie: ['4','1','4','2'] => ["41", "42"]
let hex_char = &char_vec
.chunks(2)
.map(|chunk| chunk.iter().collect::<String>())
.collect::<Vec<_>>();
let input_str = hex_to_string(&hex_char);
// Convert hex_char : Vec<String> into Vec<u8>
// Warning: Panic if not base16
// See this with .map_err if we need to improve :
@ -123,6 +124,10 @@ fn main() {
.map(|i| u8::from_str_radix(i, 16).unwrap())
.collect();
// Convert Hexadecimal Vec<String> to String
// ie: ["41", "42"] => "AB"
let input_str = hex_to_string(&hex_char);
let output = base64_encode(&hex);
println!("→ Input string is « {} »", input);