将十六进制字符串转换为浮点数的最直接方法是什么?(不使用第 3 方板条箱)。
Rust 是否提供了一些与 Python 相同的功能? struct.unpack('!f', bytes.fromhex('41973333'))
如果没有外部板条箱,这很容易:
fn main() {
// Hex string to 4-bytes, aka. u32
let bytes = u32::from_str_radix("41973333", 16).unwrap();
// Reinterpret 4-bytes as f32:
let float = unsafe { std::mem::transmute::<u32, f32>(bytes) };
// Print 18.9
println!("{}", float);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1616 次 |
| 最近记录: |