From 9bcd27374f5e9fcc10e1736ac453fe0c934abaf9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maxime=20=E2=80=9Cpep=E2=80=9D=20Buquet?= Date: Tue, 30 Jun 2020 00:47:43 +0200 Subject: [PATCH] pkstrings: convert example MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Maxime “pep” Buquet --- pkstrings/examples/convert.rs | 52 +++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 pkstrings/examples/convert.rs diff --git a/pkstrings/examples/convert.rs b/pkstrings/examples/convert.rs new file mode 100644 index 0000000..001ec86 --- /dev/null +++ b/pkstrings/examples/convert.rs @@ -0,0 +1,52 @@ +// Copyright (C) 2020 "Maxime “pep” Buquet " +// +// This program is free software: you can redistribute it and/or modify it +// under the terms of the GNU Affero General Public License as published by the +// Free Software Foundation, either version 3 of the License, or (at your +// option) any later version. +// +// This program is distributed in the hope that it will be useful, but WITHOUT +// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +// FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License +// for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see . + +use std::env::args; +use std::convert::TryFrom; +use std::process::exit; + +use pkstrings::PKString; + +fn main() { + let args: Vec = args().collect(); + + if args.len() < 2 { + eprintln!("usage: {} |rev ", args[0]); + exit(1); + } + + if args.len() == 2 { + // Convert String to PKString and then to Vec + let tmp: Vec = PKString::try_from(args[1].clone()).unwrap().into(); + println!("{:#02x?}", tmp); + } else if args.len() == 3 && args[1] == String::from("rev") { + // Consure chars 2 by 2 and convert to u8. + let mut vec: Vec = vec![]; + + args[2].chars().fold(vec![], |mut acc, chr| { + acc.push(chr); + + if acc.len() == 2 { + let ord = format!("{}{}", acc[0], acc[1]); + vec.push(u8::from_str_radix(&ord, 16).unwrap()); + acc = vec![]; + } + + acc + }); + + println!("{}", PKString::try_from(vec.as_slice()).unwrap()); + } +}