我想把我的字节数组转换成一个u64。
例如,b"00"应该返回0u 64,b"0a"应该返回10u 64。
我正在研究区块链,所以我必须找到有效率的东西。
例如,我的当前功能根本没有效率。
let number_string = String::from_utf8_lossy(&my_bytes_array)
.to_owned()
.to_string();
let number = u64::from_str_radix(&number_string , 16).unwrap();我也试过
let number = u64::from_le_bytes(my_bytes_array);但是我得到了一个错误mismatched types expected array [u8; 8], found &[u8]
发布于 2022-03-16 20:21:02
怎么样?
pub fn hex_to_u64(x: &[u8]) -> Option<u64> {
let mut result: u64 = 0;
for i in x {
result *= 16;
result += (*i as char).to_digit(16)? as u64;
}
Some(result)
}https://stackoverflow.com/questions/71501726
复制相似问题