cl-intbytes

2015-09-24

Encode/decode any-base integers and byte arrays interchangeably.

Upstream URL

github.com/EuAndreh/cl-intbytes

Author

André Miranda

Maintainer

André Miranda

License

LLGPL
README

cl-intbytes - Convert between any-base integers and byte arrays interchangeably

Quicklisp Build Status Circle CI Coverage Status

Inspired by cl-mongo.

Usage

* (ql:quickload :cl-intbytes)
; => (:CL-INTBYTES)
* (use-package :intbytes)
; => T

For easy/ready encoding/decoding, use int32->octets/octets->int32 and int64->octets/octets->int64. All accept an &optional (start 0) value:

* (int32->octets 84215045)
; => #(5 5 5 5)
* (octets->int32 *)
; => 84215045
* (octets->int32 #(0 5 5 5 5) 1)
; => 84215045
* (int64->octets 578437695752307201)
; => #(1 2 3 4 5 6 7 8)
* (octets->int64 *)
; => 578437695752307201

For unsigned values, there are equivalent unsigned functions octets->uint32 and octets->uint64:

* (int32->octets -123)
; => #(133 255 255 255)
* (octets->uint32 *)
; => 4294967173
* (int64->octets -1)
; => #(255 255 255 255 255 255 255 255)
* (octets->uint64 *)
; => 18446744073709551615
* (octets->int64 **)
; => -1

You can create your own functions with int->octets and octets->int:

* (defun int16->octets (int16)
    (int->octets int16 2))
; => INT16->OCTETS
* (defun octets->int16 (array &optional (start 0))
    (octets->int array 2 start))
; => OCTETS->INT16
* (defun octets->uint16 (array &optional (start 0))
    (octets->uint array 2 start))
; => OCTETS->UINT16
* (int16->octets -3)
; => #(253 255)
* (octets->uint16 *)
; => 65533
* (octets->int16 **)
; => -3
* (octets->int16 #(0 253 255) 1)
; => -3

If you want to encode floats to bytes, use ieee-floats:

* (int64->octets (ieee-floats:encode-float64 1.5d0))
; => #(0 0 0 0 0 0 248 63)
* (ieee-floats:decode-float64 (octets->int64 *))
; => 1.5d0

Dependencies

This library depends on fast-io.

The test package uses the prove test library.

cl-intbytes is used by cl-BSON.

Installation

(ql:quickload :cl-intbytes)

Bugs

If you find any bug or inconsistency in the code, or if you find it too hard to use, please, feel free to open an issue.

Tests

This library is tested under SBCL and CCL Common Lisp implementations.

To run all the defined tests, use:

* (asdf:test-system :cl-intbytes)
; prints lots of (colorful) stuff...
; => T

Tests are ran with Travis CI and Circle CI using cl-travis, CIM, cl-coveralls and Roswell. Check it out!

Author

License

LLGPL.

Dependencies (2)

  • fast-io
  • prove

Dependents (1)

  • GitHub
  • Quicklisp