Skip to content

Instantly share code, notes, and snippets.

@literadix
Last active May 18, 2018 08:30
Show Gist options
  • Save literadix/0f34fd9ca5ecd875d8e8bccc49f33568 to your computer and use it in GitHub Desktop.
Save literadix/0f34fd9ca5ecd875d8e8bccc49f33568 to your computer and use it in GitHub Desktop.
Parse hex value from command line arguments in rust
// For Masud
use std::env;
fn main() {
let args: Vec<String> = env::args().collect();
match args.len() {
2 => {
match u32::from_str_radix(&args[1], 16) {
Ok(n) => {
println!("{}", unsafe { std::mem::transmute::<u32, i32>(n) });
}
Err(err) => println!("Error: {:?}", err),
}
}
_ => {
println!("usage example: hexvalue ABCD");
}
}
}
@literadix
Copy link
Author

literadix commented May 18, 2018

Improve error handling even more:

https://blog.burntsushi.net/rust-error-handling/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment