将固定长度的 Vec<u8> 的十六进制字符转换为 int

Turning a fixed length Vec<u8> of hex only chars into an int

如何将固定长度 Vec<u8> 的十六进制字符转换为 u32

let input: Vec<u8> = vec!['b','e','e','f']; // Pretend this works
let output: u32 = // Do some magic (Probably with serialize)
assert!(0xbeef == output);

这可能不是最有效的方式,但可能是最明显的方式。首先将字节转换为字符串,然后将该字符串转换为数字:

生锈 1.0

fn main() {
    let input: Vec<u8> = b"beef"[..].to_owned();
    let s = String::from_utf8(input).unwrap();
    let v = u32::from_str_radix(&s, 16).unwrap();
    println!("{:x}", v);
    assert!(0xbeef == v);
}

原创

use std::num;

fn main() {
    let input: Vec<u8> = vec!['b' as u8, 'e' as u8, 'e' as u8, 'f' as u8];
    let s = String::from_utf8(input).unwrap();
    let v: u32 = num::from_str_radix(&*s, 16).unwrap();
    println!("{:x}", v);
    assert!(0xbeef == v);
}

Is there a way to do this with serialize?

我不知道。 Serialize 具有 FromHex,可将 &str 转换为 Vec<u8>。然后你仍然需要以某种方式将这些字节转换为数字:

extern crate serialize;
use serialize::hex::FromHex;

fn main() {
    let input: Vec<u8> = vec!['b' as u8, 'e' as u8, 'e' as u8, 'f' as u8];
    let s = String::from_utf8(input).unwrap();
    let bytes: Vec<u8> = s.from_hex().unwrap();

    let v = bytes.into_iter().fold(0u32, |val, byte| val << 8 | byte as u32);

    println!("{:x}", v);
    assert!(0xbeef == v);
}

到那时,直接从代码点转换为数字并自己进行求和可能更容易:

fn dehex(c: u8) -> u8 {
    match c {
        b'0'...b'9' => c - b'0',
        b'a'...b'f' => c - b'a' + 10,
        b'A'...b'F' => c - b'A' + 10,
        _ => panic!("Do something better here"),
    }
}

fn main() {
    let input: Vec<u8> = vec![b'b', b'e', b'e', b'f'];
    let v = input.into_iter().fold(0u32, |val, char| (val << 4) | dehex(char) as u32);
    println!("{:x}", v);
    assert!(0xbeef == v);
}