endian-sequence
Description
endian-sequence is a library of endian-specific procedures for converting byte sequences to numeric values and vectors.
An endian sequence associates a byte sequence (from the byte-sequence egg) with a byte order (MSB or LSB).
This egg was called endian-blob in CHICKEN 4 and 5. In CHICKEN 6 it was renamed to endian-sequence and rebuilt on top of byte-sequence and bytevectors. All procedure names that contained endian-blob now contain endian-sequence instead; for example, endian-blob->uint2 is now endian-sequence->uint2 and byte-blob->endian-blob is now byte-sequence->endian-sequence.
Author
Ivan Raikov
Repository
https://github.com/iraikov/chicken-endian-blob
Requirements
Usage
(import srfi-4 byte-sequence endian-sequence) (define s (uint2->endian-sequence #x0102 MSB)) (byte-sequence->list (endian-sequence->byte-sequence s)) ; => (1 2) (endian-sequence->uint2 s) ; => 258 (endian-sequence->u16vector (u16vector->endian-sequence (u16vector 100 200) LSB)) ; => #u16(100 200)
Library Procedures
Predicates and constants
[procedure] (endian-sequence? X) => BOOLReturns #t if the given object is an endian sequence, #f otherwise.
[constant] MSB[constant] LSB
These constants specify most-significant or least-significant byte order, respectively.
Converting to and from byte sequences
[procedure] (byte-sequence->endian-sequence BYTE-SEQUENCE [BYTE-ORDER]) => ENDIAN-SEQUENCEReturns an endian sequence containing the given byte sequence. Optional argument BYTE-ORDER is one of MSB or LSB. Default is MSB.
[procedure] (endian-sequence->byte-sequence ENDIAN-SEQUENCE) => BYTE-SEQUENCEReturns the byte sequence contained in the given endian sequence.
[procedure] (endian-sequence-length ENDIAN-SEQUENCE) => INTEGERReturns the length of the given endian sequence in bytes.
[procedure] (endian-sequence-mode ENDIAN-SEQUENCE) => BYTE-ORDERReturns the byte order (MSB or LSB) of the given endian sequence.
Converting to and from numbers and numeric vectors
Signed integers
[procedure] (endian-sequence->sint1 ENDIAN-SEQUENCE) => NUMBER[procedure] (endian-sequence->sint2 ENDIAN-SEQUENCE) => NUMBER
[procedure] (endian-sequence->sint4 ENDIAN-SEQUENCE) => NUMBER
[procedure] (sint1->endian-sequence NUMBER [MODE]) => ENDIAN-SEQUENCE
[procedure] (sint2->endian-sequence NUMBER [MODE]) => ENDIAN-SEQUENCE
[procedure] (sint4->endian-sequence NUMBER [MODE]) => ENDIAN-SEQUENCE
These procedures convert between endian sequences and signed integers of size 1, 2, or 4 bytes, respectively. Exceptions are thrown if the given endian sequences are of incorrect size, or if the given numbers are too big to fit in the specified size. Optional argument MODE indicates the endianness of the resulting endian sequence and can be one of MSB or LSB. Default is MSB.
Unsigned integers
[procedure] (endian-sequence->uint1 ENDIAN-SEQUENCE) => NUMBER[procedure] (endian-sequence->uint2 ENDIAN-SEQUENCE) => NUMBER
[procedure] (endian-sequence->uint4 ENDIAN-SEQUENCE) => NUMBER
[procedure] (uint1->endian-sequence NUMBER [MODE]) => ENDIAN-SEQUENCE
[procedure] (uint2->endian-sequence NUMBER [MODE]) => ENDIAN-SEQUENCE
[procedure] (uint4->endian-sequence NUMBER [MODE]) => ENDIAN-SEQUENCE
These procedures convert between endian sequences and unsigned integers of size 1, 2, or 4 bytes, respectively. Exceptions are thrown if the given endian sequences are of incorrect size, or if the given numbers are too big to fit in the specified size. Optional argument MODE indicates the endianness of the resulting endian sequence and can be one of MSB or LSB. Default is MSB.
IEEE floating point numbers
[procedure] (endian-sequence->ieee_float32 ENDIAN-SEQUENCE) => NUMBER[procedure] (endian-sequence->ieee_float64 ENDIAN-SEQUENCE) => NUMBER
[procedure] (ieee_float32->endian-sequence NUMBER [MODE]) => ENDIAN-SEQUENCE
[procedure] (ieee_float64->endian-sequence NUMBER [MODE]) => ENDIAN-SEQUENCE
These procedures convert between endian sequences and IEEE floating point numbers of single or double precision, respectively. Infinities and NaNs are preserved in both directions. Exceptions are thrown if the given endian sequences are of incorrect size (4 bytes for single precision, 8 bytes for double precision). Optional argument MODE indicates the endianness of the resulting endian sequence and can be one of MSB or LSB. Default is MSB.
SRFI-4 vectors
[procedure] (endian-sequence->s8vector ENDIAN-SEQUENCE) => S8VECTOR[procedure] (endian-sequence->s16vector ENDIAN-SEQUENCE) => S16VECTOR
[procedure] (endian-sequence->s32vector ENDIAN-SEQUENCE) => S32VECTOR
[procedure] (endian-sequence->u8vector ENDIAN-SEQUENCE) => U8VECTOR
[procedure] (endian-sequence->u16vector ENDIAN-SEQUENCE) => U16VECTOR
[procedure] (endian-sequence->u32vector ENDIAN-SEQUENCE) => U32VECTOR
[procedure] (endian-sequence->f32vector ENDIAN-SEQUENCE) => F32VECTOR
[procedure] (endian-sequence->f64vector ENDIAN-SEQUENCE) => F64VECTOR
[procedure] (s8vector->endian-sequence S8VECTOR [MODE]) => ENDIAN-SEQUENCE
[procedure] (s16vector->endian-sequence S16VECTOR [MODE]) => ENDIAN-SEQUENCE
[procedure] (s32vector->endian-sequence S32VECTOR [MODE]) => ENDIAN-SEQUENCE
[procedure] (u8vector->endian-sequence U8VECTOR [MODE]) => ENDIAN-SEQUENCE
[procedure] (u16vector->endian-sequence U16VECTOR [MODE]) => ENDIAN-SEQUENCE
[procedure] (u32vector->endian-sequence U32VECTOR [MODE]) => ENDIAN-SEQUENCE
[procedure] (f32vector->endian-sequence F32VECTOR [MODE]) => ENDIAN-SEQUENCE
[procedure] (f64vector->endian-sequence F64VECTOR [MODE]) => ENDIAN-SEQUENCE
These procedures convert between endian sequences and the corresponding SRFI-4 vector type. When converting to a vector, the length of the endian sequence must be a positive multiple of the element size of that vector type. Optional argument MODE indicates the endianness of the resulting endian sequence and can be one of MSB or LSB. Default is MSB.
Version History
- 3.0 Ported to CHICKEN 6; renamed from endian-blob to endian-sequence, now built on byte-sequence and bytevectors; fixed decoding of IEEE infinities and NaNs
- 2.0 Ported to CHICKEN 5
- 1.4 Removed dependency on ansidecl.h (thanks to Peter Bex)
- 1.3 Added procedure endian-blob-length
- 1.2 Fixed a bug in uint2->endian-blob (thanks to Shawn Rutledge)
- 1.1 Some small optimizations
- 1.0 Initial release
License
Copyright 2009-2026 Ivan Raikov.
endian-sequence is based on routines from the C++ advanced I/O library and TIFF reader written by Oleg Kiselyov, as well as the floating-point I/O routines from GDB.
endian-sequence is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
endian-sequence is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
A full copy of the GPL license can be found at <http://www.gnu.org/licenses/>.