Next: , Previous: , Up: stdlib bytevector   [Index]


5.2.4 Operations on integers of arbitrary size

Procedure: bytevector-uint-ref bytevector k endianness size
Procedure: bytevector-sint-ref bytevector k endianness size
Procedure: bytevector-uint-set! bytevector k n endianness size
Procedure: bytevector-sint-set! bytevector k n endianness size

size must be a positive exact integer object. k, …, k+size-1 must be valid indices of bytevector.

The bytevector-uint-ref procedure retrieves the exact integer object corresponding to the unsigned representation of size size and specified by endianness at indices k, …, k + size - 1.

The bytevector-sint-ref procedure retrieves the exact integer object corresponding to the two’s–complement representation of size size and specified by endianness at indices k, …, k+size-1.

For bytevector-uint-set!, n must be an exact integer object in the interval (0, …, 256^(size-1)).

The bytevector-uint-set! procedure stores the unsigned representation of size size and specified by endianness into bytevector at indices k, …, k + size - 1.

For bytevector-sint-set!, n must be an exact integer object in the interval [-(256^{size})/2, (256^{size})/2-1]. bytevector-sint-set! stores the two’s–complement representation of size size and specified by endianness into bytevector at indices k, …, k + size - 1.

The ...-set! procedures return unspecified values.

(define b (make-bytevector 16 -127))

(bytevector-uint-set! b 0 (- (expt 2 128) 3) (endianness little) 16)

(bytevector-uint-ref b 0 (endianness little) 16)
⇒ #xfffffffffffffffffffffffffffffffd

(bytevector-sint-ref b 0 (endianness little) 16)
⇒ -3

(bytevector->u8-list b)
⇒ (253 255 255 255 255 255 255 255
    255 255 255 255 255 255 255 255)

(bytevector-uint-set! b 0 (- (expt 2 128) 3) (endianness big) 16)

(bytevector-uint-ref b 0 (endianness big) 16)
⇒ #xfffffffffffffffffffffffffffffffd

(bytevector-sint-ref b 0 (endianness big) 16)
⇒ -3

(bytevector->u8-list b)
⇒ (255 255 255 255 255 255 255 255
    255 255 255 255 255 255 255 253))
Procedure: bytevector->uint-list bytevector endianness size
Procedure: bytevector->sint-list bytevector endianness size
Procedure: uint-list->bytevector list endianness size
Procedure: sint-list->bytevector list endianness size

size must be a positive exact integer object.

For uint-list->bytevector, list must be a list of exact integer objects in the interval (0, …, (256^size)-1).

For sint-list->bytevector, list must be a list of exact integer objects in the interval (-(256^size)/2, …, (256^size)/2-1).

The length of bytevector must be divisible by size.

These procedures convert between lists of integer objects and their consecutive representations according to size and endianness in the bytevector objects in the same way as bytevector->u8-list and u8-list->bytevector do for one–byte representations.

(let ((b (u8-list->bytevector '(1 2 3 255 1 2 1 2))))
  (bytevector->sint-list b (endianness little) 2))
⇒ (513 -253 513 513)

(let ((b (u8-list->bytevector '(1 2 3 255 1 2 1 2))))
  (bytevector->uint-list b (endianness little) 2))
⇒ (513 65283 513 513)

Next: , Previous: , Up: stdlib bytevector   [Index]