fhex 2.0.0

Hex float conversion: ToHex for formatting, FromHex for parsing. IEEE 754 hexadecimal format (0x1.8p+1).
Documentation
  • Coverage
  • 100%
    5 out of 5 items documented3 out of 5 items with examples
  • Size
  • Source code size: 43.72 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 1.59 MB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 12s Average build duration of successful builds.
  • all releases: 11s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • rvagg/fhex
    2 0 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • rvagg

fhex

Crates.io Documentation

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.

Documentation · Crates.io

Usage

[dependencies]
fhex = "2.0"
use fhex::{ToHex, FromHex};

// Formatting
let hex = 3.0_f64.to_hex();
assert_eq!(hex, "0x1.8p+1");

// Parsing
let value = f64::from_hex("0x1.8p+1").unwrap();
assert_eq!(value, 3.0);

// Round-trip
let original = std::f64::consts::PI;
let roundtrip = f64::from_hex(&original.to_hex()).unwrap();
assert_eq!(original, roundtrip);

Format

Floating point numbers are represented as ±0xh.hhhp±d, where:

  • ± is the sign (- for negative, omitted for positive)
  • 0x is the hex prefix
  • h.hhh is the significand in hexadecimal
  • p±d is the exponent in decimal (base 2)

Special values:

  • ±0x0p+0 for zero
  • ±inf for infinity
  • nan for quiet NaN
  • nan: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 ±Inf and NaN (capitalised), zero-pads exponent to 2 digits
  • C++ std::hexfloat: No special NaN payload handling

License

Apache-2.0