6.6.12.3 Interpreting Bytevector Contents as Integers

The contents of a bytevector can be interpreted as a sequence of integers of any given size, sign, and endianness.

(let ((bv (make-bytevector 4)))
  (bytevector-u8-set! bv 0 #x12)
  (bytevector-u8-set! bv 1 #x34)
  (bytevector-u8-set! bv 2 #x56)
  (bytevector-u8-set! bv 3 #x78)

  (map (lambda (number)
         (number->string number 16))
       (list (bytevector-u8-ref bv 0)
             (bytevector-u16-ref bv 0 (endianness big))
             (bytevector-u32-ref bv 0 (endianness little)))))

⇒ ("12" "1234" "78563412")

The most generic procedures to interpret bytevector contents as integers are described below.

Scheme Procedure: bytevector-uint-ref bv index endianness size
C Function: scm_bytevector_uint_ref (bv, index, endianness, size)

Return the size-byte long unsigned integer at index index in bv, decoded according to endianness.

Scheme Procedure: bytevector-sint-ref bv index endianness size
C Function: scm_bytevector_sint_ref (bv, index, endianness, size)

Return the size-byte long signed integer at index index in bv, decoded according to endianness.

Scheme Procedure: bytevector-uint-set! bv index value endianness size
C Function: scm_bytevector_uint_set_x (bv, index, value, endianness, size)

Set the size-byte long unsigned integer at index to value, encoded according to endianness.

Scheme Procedure: bytevector-sint-set! bv index value endianness size
C Function: scm_bytevector_sint_set_x (bv, index, value, endianness, size)

Set the size-byte long signed integer at index to value, encoded according to endianness.

The following procedures are similar to the ones above, but specialized to a given integer size:

Scheme Procedure: bytevector-u8-ref bv index
Scheme Procedure: bytevector-s8-ref bv index
Scheme Procedure: bytevector-u16-ref bv index endianness
Scheme Procedure: bytevector-s16-ref bv index endianness
Scheme Procedure: bytevector-u32-ref bv index endianness
Scheme Procedure: bytevector-s32-ref bv index endianness
Scheme Procedure: bytevector-u64-ref bv index endianness
Scheme Procedure: bytevector-s64-ref bv index endianness
C Function: scm_bytevector_u8_ref (bv, index)
C Function: scm_bytevector_s8_ref (bv, index)
C Function: scm_bytevector_u16_ref (bv, index, endianness)
C Function: scm_bytevector_s16_ref (bv, index, endianness)
C Function: scm_bytevector_u32_ref (bv, index, endianness)
C Function: scm_bytevector_s32_ref (bv, index, endianness)
C Function: scm_bytevector_u64_ref (bv, index, endianness)
C Function: scm_bytevector_s64_ref (bv, index, endianness)

Return the unsigned n-bit (signed) integer (where n is 8, 16, 32 or 64) from bv at index, decoded according to endianness.

Scheme Procedure: bytevector-u8-set! bv index value
Scheme Procedure: bytevector-s8-set! bv index value
Scheme Procedure: bytevector-u16-set! bv index value endianness
Scheme Procedure: bytevector-s16-set! bv index value endianness
Scheme Procedure: bytevector-u32-set! bv index value endianness
Scheme Procedure: bytevector-s32-set! bv index value endianness
Scheme Procedure: bytevector-u64-set! bv index value endianness
Scheme Procedure: bytevector-s64-set! bv index value endianness
C Function: scm_bytevector_u8_set_x (bv, index, value)
C Function: scm_bytevector_s8_set_x (bv, index, value)
C Function: scm_bytevector_u16_set_x (bv, index, value, endianness)
C Function: scm_bytevector_s16_set_x (bv, index, value, endianness)
C Function: scm_bytevector_u32_set_x (bv, index, value, endianness)
C Function: scm_bytevector_s32_set_x (bv, index, value, endianness)
C Function: scm_bytevector_u64_set_x (bv, index, value, endianness)
C Function: scm_bytevector_s64_set_x (bv, index, value, endianness)

Store value as an n-bit (signed) integer (where n is 8, 16, 32 or 64) in bv at index, encoded according to endianness.

Finally, a variant specialized for the host’s endianness is available for each of these functions (with the exception of the u8 and s8 accessors, as endianness is about byte order and there is only 1 byte):

Scheme Procedure: bytevector-u16-native-ref bv index
Scheme Procedure: bytevector-s16-native-ref bv index
Scheme Procedure: bytevector-u32-native-ref bv index
Scheme Procedure: bytevector-s32-native-ref bv index
Scheme Procedure: bytevector-u64-native-ref bv index
Scheme Procedure: bytevector-s64-native-ref bv index
C Function: scm_bytevector_u16_native_ref (bv, index)
C Function: scm_bytevector_s16_native_ref (bv, index)
C Function: scm_bytevector_u32_native_ref (bv, index)
C Function: scm_bytevector_s32_native_ref (bv, index)
C Function: scm_bytevector_u64_native_ref (bv, index)
C Function: scm_bytevector_s64_native_ref (bv, index)

Return the unsigned n-bit (signed) integer (where n is 8, 16, 32 or 64) from bv at index, decoded according to the host’s native endianness.

Scheme Procedure: bytevector-u16-native-set! bv index value
Scheme Procedure: bytevector-s16-native-set! bv index value
Scheme Procedure: bytevector-u32-native-set! bv index value
Scheme Procedure: bytevector-s32-native-set! bv index value
Scheme Procedure: bytevector-u64-native-set! bv index value
Scheme Procedure: bytevector-s64-native-set! bv index value
C Function: scm_bytevector_u16_native_set_x (bv, index, value)
C Function: scm_bytevector_s16_native_set_x (bv, index, value)
C Function: scm_bytevector_u32_native_set_x (bv, index, value)
C Function: scm_bytevector_s32_native_set_x (bv, index, value)
C Function: scm_bytevector_u64_native_set_x (bv, index, value)
C Function: scm_bytevector_s64_native_set_x (bv, index, value)

Store value as an n-bit (signed) integer (where n is 8, 16, 32 or 64) in bv at index, encoded according to the host’s native endianness.