chickadee » endian-sequence

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

endian-sequence? Xprocedure

Returns #t if the given object is an endian sequence, #f otherwise.

MSBconstant
LSBconstant

These constants specify most-significant or least-significant byte order, respectively.

Converting to and from byte sequences

byte-sequence->endian-sequence BYTE-SEQUENCE #!optional BYTE-ORDERprocedure

Returns an endian sequence containing the given byte sequence. Optional argument BYTE-ORDER is one of MSB or LSB. Default is MSB.

endian-sequence->byte-sequence ENDIAN-SEQUENCEprocedure

Returns the byte sequence contained in the given endian sequence.

endian-sequence-length ENDIAN-SEQUENCEprocedure

Returns the length of the given endian sequence in bytes.

endian-sequence-mode ENDIAN-SEQUENCEprocedure

Returns the byte order (MSB or LSB) of the given endian sequence.

Converting to and from numbers and numeric vectors

Signed integers
endian-sequence->sint1 ENDIAN-SEQUENCEprocedure
endian-sequence->sint2 ENDIAN-SEQUENCEprocedure
endian-sequence->sint4 ENDIAN-SEQUENCEprocedure
sint1->endian-sequence NUMBER #!optional MODEprocedure
sint2->endian-sequence NUMBER #!optional MODEprocedure
sint4->endian-sequence NUMBER #!optional MODEprocedure

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
endian-sequence->uint1 ENDIAN-SEQUENCEprocedure
endian-sequence->uint2 ENDIAN-SEQUENCEprocedure
endian-sequence->uint4 ENDIAN-SEQUENCEprocedure
uint1->endian-sequence NUMBER #!optional MODEprocedure
uint2->endian-sequence NUMBER #!optional MODEprocedure
uint4->endian-sequence NUMBER #!optional MODEprocedure

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
endian-sequence->ieee_float32 ENDIAN-SEQUENCEprocedure
endian-sequence->ieee_float64 ENDIAN-SEQUENCEprocedure
ieee_float32->endian-sequence NUMBER #!optional MODEprocedure
ieee_float64->endian-sequence NUMBER #!optional MODEprocedure

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
endian-sequence->s8vector ENDIAN-SEQUENCEprocedure
endian-sequence->s16vector ENDIAN-SEQUENCEprocedure
endian-sequence->s32vector ENDIAN-SEQUENCEprocedure
endian-sequence->u8vector ENDIAN-SEQUENCEprocedure
endian-sequence->u16vector ENDIAN-SEQUENCEprocedure
endian-sequence->u32vector ENDIAN-SEQUENCEprocedure
endian-sequence->f32vector ENDIAN-SEQUENCEprocedure
endian-sequence->f64vector ENDIAN-SEQUENCEprocedure
s8vector->endian-sequence S8VECTOR #!optional MODEprocedure
s16vector->endian-sequence S16VECTOR #!optional MODEprocedure
s32vector->endian-sequence S32VECTOR #!optional MODEprocedure
u8vector->endian-sequence U8VECTOR #!optional MODEprocedure
u16vector->endian-sequence U16VECTOR #!optional MODEprocedure
u32vector->endian-sequence U32VECTOR #!optional MODEprocedure
f32vector->endian-sequence F32VECTOR #!optional MODEprocedure
f64vector->endian-sequence F64VECTOR #!optional MODEprocedure

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

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/>.

Contents »