bitty/num
Numeric parsers for fixed-width integers, floats, and variable-length
encodings. Includes single-byte (u8, i8) and multi-byte types with
endianness control.
Types
Values
pub fn f32(endian: Endian) -> bitty.Parser(Float)
Parse a 32-bit IEEE 754 float with the given endianness.
pub fn f64(endian: Endian) -> bitty.Parser(Float)
Parse a 64-bit IEEE 754 double with the given endianness.
pub fn i16(endian: Endian) -> bitty.Parser(Int)
Parse a 16-bit signed integer (two’s complement) with the given endianness.
pub fn i32(endian: Endian) -> bitty.Parser(Int)
Parse a 32-bit signed integer (two’s complement) with the given endianness.
pub fn i64(endian: Endian) -> bitty.Parser(Int)
Parse a 64-bit signed integer (two’s complement) with the given endianness. On the JavaScript target, values outside the safe integer range (-2^53 + 1 to 2^53 - 1) may lose precision.
pub fn int_bytes_twos_complement(
bytes count: Int,
) -> bitty.Parser(BitArray)
Read count bytes as a two’s complement signed integer BitArray,
stripping redundant leading zero bytes while preserving the sign bit.
Returns the normalized raw bytes, not a decoded Int.
Useful for ASN.1 DER integer encoding.
pub fn u16(endian: Endian) -> bitty.Parser(Int)
Parse a 16-bit unsigned integer with the given endianness.
let assert Ok(val) =
bitty.run(num.u16(num.BigEndian), on: <<0x01, 0x00>>)
assert val == 256
pub fn u32(endian: Endian) -> bitty.Parser(Int)
Parse a 32-bit unsigned integer with the given endianness.
pub fn u64(endian: Endian) -> bitty.Parser(Int)
Parse a 64-bit unsigned integer with the given endianness. On the JavaScript target, values above 2^53 - 1 (9,007,199,254,740,991) may lose precision due to IEEE 754 double-precision limitations.
pub fn uint_bytes(bytes count: Int) -> bitty.Parser(BitArray)
Read count bytes as a raw BitArray representing an unsigned integer.
Unlike bytes.take, this is intended for numeric byte sequences
(e.g. ASN.1 DER integer encodings) where the bytes represent a single
big-endian value.
pub fn var_u32() -> bitty.Parser(Int)
Parse an LEB128-encoded unsigned 32-bit variable-length integer. Consumes at most 5 bytes.
let assert Ok(val) = bitty.run(num.var_u32(), on: <<0xAC, 0x02>>)
assert val == 300
pub fn var_u64() -> bitty.Parser(Int)
Parse an LEB128-encoded unsigned 64-bit variable-length integer. Consumes at most 10 bytes. On the JavaScript target, values above 2^53 - 1 (9,007,199,254,740,991) may lose precision due to IEEE 754 double-precision limitations.