-1

Why is my program generating

thread 'main' panicked at 'called Result::unwrap() on an Err value: ParseIntError { kind: InvalidDigit }', src/main.rs:68:54 note: run with RUST_BACKTRACE=1 environment variable to display a backtrace

when I enter the generated hash it panics instead of passing control to if {..}.

fn main() {
    println!("Hello user! Please cast your input ");
    let userinput: u64 = user_input();
    let mut hasher_two = DefaultHasher::new();
    let vecdata = [0x09, 0x06, 0xba, 0x67, 0x76];
    hasher_two.write(&vecdata);
    let final_check = hasher_two.finish();

    if final_check == userinput {
        correct_file_creation();
    } else {
        wrong_file_creation();
    }
}

pub fn user_input() -> u64 {
    let mut userinput = String::new();
    std::io::stdin().read_line(&mut userinput).unwrap();
    let userinputinteger: i32 = userinput.trim().parse().unwrap();
    return userinputinteger as u64;
}
Stefan Zobel
  • 3,182
  • 7
  • 28
  • 38
ElementalX
  • 111
  • 1
  • 7
  • 1
    You didn't show the relevant code or say what the input is. Post a [mcve]. – interjay Feb 01 '22 at 16:56
  • The input is : 66d3f00739bafb9d – ElementalX Feb 01 '22 at 16:57
  • 2
    Maybe you want to fix the indentation of your code to make it more readable. – RedX Feb 01 '22 at 16:58
  • 2
    And what do you expect to happen when you parse that into a `ì32`? – interjay Feb 01 '22 at 16:58
  • Two points: first `parse::` parses _decimal_ numbers but your input is hexadecimal. Second `66d3f00739bafb9d` doesn't fit in an `i32` (and why go through an `i32` in the first place if you want to return a `u64` anyway?) → `return u64::from_str_radix (userinput.trim(), 16).unwrap()` – Jmb Feb 02 '22 at 07:45

1 Answers1

0

You need to be more specific, since you're not parsing a normal number.

You'll need to tell rust that you want to parse a hex string.

See this answer on how to do that:

Converting a hexadecimal string to a decimal integer

I do not know anything about rust but your error message suggests that because the error is in ParseIntError with a second clue in that error message: kind: InvalidDigit which means that it has no idea what an a or f is doing in your string that's supposed to be an integer.

RedX
  • 14,749
  • 1
  • 53
  • 76