Next: , Previous: Byte, Up: Data Structures


7.1.7 Byte/Number Conversions

(require 'byte-number) The multi-byte sequences produced and used by numeric conversion routines are always big-endian. Endianness can be changed during reading and writing bytes using read-bytes and write-bytes See read-bytes.

The sign of the length argument to bytes/integer conversion procedures determines the signedness of the number.

— Function: bytes->integer bytes n

Converts the first (abs n) bytes of big-endian bytes array to an integer. If n is negative then the integer coded by the bytes are treated as two's-complement (can be negative).

          (bytes->integer (bytes   0   0   0  15) -4)   ⇒          15
          (bytes->integer (bytes   0   0   0  15)  4)   ⇒          15
          (bytes->integer (bytes 255 255 255 255) -4)   ⇒          -1
          (bytes->integer (bytes 255 255 255 255)  4)   ⇒  4294967295
          (bytes->integer (bytes 128   0   0   0) -4)   ⇒ -2147483648
          (bytes->integer (bytes 128   0   0   0)  4)   ⇒  2147483648
— Function: integer->bytes n len

Converts the integer n to a byte-array of (abs n) bytes. If n and len are both negative, then the bytes in the returned array are coded two's-complement.

          (bytes->list (integer->bytes          15 -4))   ⇒ (0 0 0 15)
          (bytes->list (integer->bytes          15  4))   ⇒ (0 0 0 15)
          (bytes->list (integer->bytes          -1 -4))   ⇒ (255 255 255 255)
          (bytes->list (integer->bytes  4294967295  4))   ⇒ (255 255 255 255)
          (bytes->list (integer->bytes -2147483648 -4))   ⇒ (128 0 0 0)
          (bytes->list (integer->bytes  2147483648  4))   ⇒ (128 0 0 0)
— Function: bytes->ieee-float bytes

bytes must be a 4-element byte-array. bytes->ieee-float calculates and returns the value of bytes interpreted as a big-endian IEEE 4-byte (32-bit) number.

     (bytes->ieee-float (bytes    0    0 0 0))  ⇒  0.0
     (bytes->ieee-float (bytes #x80    0 0 0))  ⇒ -0.0
     (bytes->ieee-float (bytes #x40    0 0 0))  ⇒  2.0
     (bytes->ieee-float (bytes #x40 #xd0 0 0))  ⇒  6.5
     (bytes->ieee-float (bytes #xc0 #xd0 0 0))  ⇒ -6.5
     
     (bytes->ieee-float (bytes    0 #x80 0 0))  ⇒ 11.754943508222875e-39
     (bytes->ieee-float (bytes    0 #x40 0 0))  ⇒  5.877471754111437e-39
     (bytes->ieee-float (bytes    0    0 0 1))  ⇒  1.401298464324817e-45
     
     (bytes->ieee-float (bytes #xff #x80 0 0))  ⇒ -inf.0
     (bytes->ieee-float (bytes #x7f #x80 0 0))  ⇒ +inf.0
     (bytes->ieee-float (bytes #x7f #x80 0 1))  ⇒  0/0
     (bytes->ieee-float (bytes #x7f #xc0 0 0))  ⇒  0/0
— Function: bytes->ieee-double bytes

bytes must be a 8-element byte-array. bytes->ieee-double calculates and returns the value of bytes interpreted as a big-endian IEEE 8-byte (64-bit) number.

     (bytes->ieee-double (bytes    0    0 0 0 0 0 0 0))  ⇒  0.0
     (bytes->ieee-double (bytes #x80    0 0 0 0 0 0 0))  ⇒ -0.0
     (bytes->ieee-double (bytes #x40    0 0 0 0 0 0 0))  ⇒  2.0
     (bytes->ieee-double (bytes #x40 #x1A 0 0 0 0 0 0))  ⇒  6.5
     (bytes->ieee-double (bytes #xC0 #x1A 0 0 0 0 0 0))  ⇒ -6.5
     
     (bytes->ieee-double (bytes 0 8 0 0 0 0 0 0)) ⇒ 11.125369292536006e-309
     (bytes->ieee-double (bytes 0 4 0 0 0 0 0 0)) ⇒  5.562684646268003e-309
     (bytes->ieee-double (bytes 0 0 0 0 0 0 0 1)) ⇒  4.0e-324
     
     (bytes->ieee-double (list->bytes '(127 239 255 255 255 255 255 255))) 179.76931348623157e306
     (bytes->ieee-double (bytes #xFF #xF0 0 0 0 0 0 0))  ⇒ -inf.0
     (bytes->ieee-double (bytes #x7F #xF0 0 0 0 0 0 0))  ⇒ +inf.0
     (bytes->ieee-double (bytes #x7F #xF8 0 0 0 0 0 0))  ⇒  0/0
— Function: ieee-float->bytes x

Returns a 4-element byte-array encoding the IEEE single-precision floating-point of x.

     (bytes->list (ieee-float->bytes  0.0))                    ⇒ (0     0 0 0)
     (bytes->list (ieee-float->bytes -0.0))                    ⇒ (128   0 0 0)
     (bytes->list (ieee-float->bytes  2.0))                    ⇒ (64    0 0 0)
     (bytes->list (ieee-float->bytes  6.5))                    ⇒ (64  208 0 0)
     (bytes->list (ieee-float->bytes -6.5))                    ⇒ (192 208 0 0)
     
     (bytes->list (ieee-float->bytes 11.754943508222875e-39))  ⇒ (  0 128 0 0)
     (bytes->list (ieee-float->bytes  5.877471754111438e-39))  ⇒ (  0  64 0 0)
     (bytes->list (ieee-float->bytes  1.401298464324817e-45))  ⇒ (  0   0 0 1)
     
     (bytes->list (ieee-float->bytes -inf.0))                  ⇒ (255 128 0 0)
     (bytes->list (ieee-float->bytes +inf.0))                  ⇒ (127 128 0 0)
     (bytes->list (ieee-float->bytes  0/0))                    ⇒ (127 192 0 0)
— Function: ieee-double->bytes x

Returns a 8-element byte-array encoding the IEEE double-precision floating-point of x.

     (bytes->list (ieee-double->bytes  0.0)) ⇒ (0     0 0 0 0 0 0 0)
     (bytes->list (ieee-double->bytes -0.0)) ⇒ (128   0 0 0 0 0 0 0)
     (bytes->list (ieee-double->bytes  2.0)) ⇒ (64    0 0 0 0 0 0 0)
     (bytes->list (ieee-double->bytes  6.5)) ⇒ (64   26 0 0 0 0 0 0)
     (bytes->list (ieee-double->bytes -6.5)) ⇒ (192  26 0 0 0 0 0 0)
     
     (bytes->list (ieee-double->bytes 11.125369292536006e-309))
                                             ⇒ (  0   8 0 0 0 0 0 0)
     (bytes->list (ieee-double->bytes  5.562684646268003e-309))
                                             ⇒ (  0   4 0 0 0 0 0 0)
     (bytes->list (ieee-double->bytes  4.0e-324))
                                             ⇒ (  0   0 0 0 0 0 0 1)
     
     (bytes->list (ieee-double->bytes -inf.0)) ⇒ (255 240 0 0 0 0 0 0)
     (bytes->list (ieee-double->bytes +inf.0)) ⇒ (127 240 0 0 0 0 0 0)
     (bytes->list (ieee-double->bytes  0/0)) ⇒ (127 248 0 0 0 0 0 0)
Byte Collation Order

The string<? ordering of big-endian byte-array representations of fixed and IEEE floating-point numbers agrees with the numerical ordering only when those numbers are non-negative.

Straighforward modification of these formats can extend the byte-collating order to work for their entire ranges. This agreement enables the full range of numbers as keys in indexed-sequential-access-method databases.

— Procedure: integer-byte-collate! byte-vector

Modifies sign bit of byte-vector so that string<? ordering of two's-complement byte-vectors matches numerical order. integer-byte-collate! returns byte-vector and is its own functional inverse.

— Function: integer-byte-collate byte-vector

Returns copy of byte-vector with sign bit modified so that string<? ordering of two's-complement byte-vectors matches numerical order. integer-byte-collate is its own functional inverse.

— Procedure: ieee-byte-collate! byte-vector

Modifies byte-vector so that string<? ordering of IEEE floating-point byte-vectors matches numerical order. ieee-byte-collate! returns byte-vector.

— Procedure: ieee-byte-decollate! byte-vector

Given byte-vector modified by ieee-byte-collate!, reverses the byte-vector modifications.

— Function: ieee-byte-collate byte-vector

Returns copy of byte-vector encoded so that string<? ordering of IEEE floating-point byte-vectors matches numerical order.

— Function: ieee-byte-decollate byte-vector

Given byte-vector returned by ieee-byte-collate, reverses the byte-vector modifications.