fhex
Hex float conversion for Rust: ToHex for formatting, FromHex for parsing.
Uses the IEEE 754 hexadecimal floating-point format (±0xh.hhhp±d)—the same format used by C's %a printf specifier, Java's Double.toHexString(), and the WebAssembly text format.
Usage
[]
= "2.0"
use ;
// Formatting
let hex = 3.0_f64.to_hex;
assert_eq!;
// Parsing
let value = f64from_hex.unwrap;
assert_eq!;
// Round-trip
let original = PI;
let roundtrip = f64from_hex.unwrap;
assert_eq!;
Format
Floating point numbers are represented as ±0xh.hhhp±d, where:
±is the sign (-for negative, omitted for positive)0xis the hex prefixh.hhhis the significand in hexadecimalp±dis the exponent in decimal (base 2)
Special values:
±0x0p+0for zero±inffor infinitynanfor quiet NaNnan:0x...for NaN with payload (signalling NaN)
Parsing Features
- Underscores for readability:
0x1_000p+0 - NaN payloads preserved:
nan:0x123 - Case-insensitive:
INF,NaN,0X1P+0 - Whitespace trimmed
Output Differences vs Other Languages
- Go's
%x: Uses±InfandNaN(capitalised), zero-pads exponent to 2 digits - C++
std::hexfloat: No special NaN payload handling
License
Apache-2.0