binary.io
- Binary I/O ¶This module provides basic procedures to perform binary I/O
of numeric data. Each datum can be read from or written to a port,
and got from or put to a uniform vector (see Uniform vectors).
For structured binary data I/O,
more convenient pack
utility is implemented
on top of this module (see binary.pack
- Packing binary data).
You might want to use this module directly if you need
speed or want a flexible control of endianness.
See also Uniform vectors, which provides binary block I/O.
Most procedures of this module take an optional endian argument,
specifying the byte order of the binary input.
It must be either one of symbols big-endian
, little-endian
,
or arm-little-endian
.
If the endian argument is omitted, the current value of the builtin
parameter default-endian
is used (see Endianness).
(For 8-bit I/O procedures like read-u8
the endian
argument has no effect, but is accepted for consistency).
{binary.io
}
Reads 8, 16, 32 or 64 bit unsigned integer from port
with specified endian, respectively. If port is omitted,
current input port is used. If port reaches EOF before
a complete integer is read, EOF is returned.
{binary.io
}
Reads 8, 16, 32 or 64 bit 2’s complement signed integer from port
with specified endian, respectively. If port is omitted,
current input port is used. If port reaches EOF before
a complete integer is read, EOF is returned.
{binary.io
}
More flexible version. Reads size-octet unsigned
or signed integer from port with specified endian.
If port reaches EOF before a complete integer is read,
EOF is returned.
{binary.io
}
Reads BER compressed integer a la X.209.
A BER compressed integer is an unsigned integer in base 128,
most significant digit first, where the high bit is set on all but the
final (least significant) byte.
{binary.io
}
Writes a nonnegative integer val as 8, 16, 32 or 64 bit
unsigned integer
to port with specified endian, respectively. Val must be within
the range of integers representable by the specified bits.
When port is omitted, current output port is used.
{binary.io
}
Writes an integer val as 8, 16, 32 or 64 bit
as 2’s complement signed integer to port with specified endian,
respectively. Val must be within
the range of integers representable by the specified bits.
When port is omitted, current output port is used.
{binary.io
}
More flexible version. Writes an integer val as unsigned
or signed integer of size bytes to port with
specified endian.
When port is omitted, current output port is used.
{binary.io
}
Writes a nonnegative integer val in BER compressed integer
to port. See read-ber-integer
above for BER format.
{binary.io
}
Reads 16, 32, or 64-bit floating point numbers, respectively.
32bit is IEEE754 single-precision, and 64bit is
IEEE754 double-precision numbers. 16-bit floating
point number consists of 1-bit sign, 5-bit exponent and
10-bit mantissa, as used in some HDR image format.
If port is omitted, current input port is used. If port reaches EOF before a complete number is read, EOF is returned.
{binary.io
}
Writes a real number val to port in 16,
32, or 64-bit floating point number, respectively.
If port is omitted, current output port is used.
In the following routines, the argument uv can be any
type of uniform vector; if it is not a u8vector
, it is
treated as if (uvector-alias <u8vector> uv)
is
called—that is, it reads directly from the memory image
that holds the uvector’s content. The pos argument
specifies the byte position from the beginning of the memory
area (it is always byte position, regardless of the uniform
vector’s element size).
{binary.io
}
Reads a number of a specific format from a uniform vector uv,
starting at a byte position pos. An error is signaled
if the specified position makes reference outside of the uniform
vector’s content. Returns the read number.
{binary.io
}
These are big-endian (be
) or little-endian (le
) specific
versions of get-*
procedures. In speed-sensitive code,
you might want to use these to avoid the overhead of optional-argument
handling.
{binary.io
}
Read size octets from uvector uv,
starting from pos-th octet, as an unsigned or signed integer,
respectively.
(get-uint 3 '#u8(1 2 3 4) 1 'big-endian) ⇒ 131884 ; #x020304 (get-sint 3 '#u9(1 2 3 #xff) 1 'little-endian) ⇒ -64766 ; sign extended #xff0302
{binary.io
}
Writes a number val into a uniform vector uv in
a specific format, starting at a byte position pos.
An error is signaled
if the specified position makes reference outside of the uniform
vector’s content.
{binary.io
}
These are big-endian (be
) or little-endian (le
) specific
versions of put-*
procedures. In speed-sensitive code,
you might want to use these to avoid the overhead of optional-argument
handling.
{binary.io
}
Write an unsigned or signed integer val into
an uvector uv starting from pos-th octet,
for size octets, respectively.
read-u8
etc. were called read-binary-uint8
etc., and
read-f32
and read-f64
were called read-binary-float
and read-binary-double
, respectively.
These old names are still supported for the backward compatibility
but their use is deprecated. The reason of the changes is
for brevity and for consistency with the uniform vectors.