This is almost Python's struct module in Clojure. You can easily converse between Clojure value and bytes stream which come from file, network ...etc.
(ns your-package
(:require [clj-struct.core :as clj-struct]))
use 'pack' function to encode clojure value to a sequence of bytes
(clj-struct/pack "c?I" \a true 32767)
;;> the output will be a sequence of byte corresponding to format "c?I" and value
The first parameter is a format (format detail see below), and rest parameters are corresponding values. In above example, format "c?I" means three Clojure value: a character, a boolean and a unsigned integer, and rest of parameters are that three values.
use "unpack" to decode a sequence of bytes to clojure values
(clj-struct/unpack "c?I" your-bytes)
;;; here the your-bytes should be a sequence of bytes read from network,
;;; file or other source. Output will be three Clojure value: a chatacter value, a boolean value and a long value
The first character of format can be use to specify the byte order, little-endian or big-endian. For example
"<f"
means Float type with little-endian order. The Byte Order is optional, default is big-endian.
There are three options:
: big-endian
After Order type is one or many Data Type. Available Data type are:
You can have a count before data type, for example "3i2H" is equal "iiiHH"
Can you improve this documentation? These fine people already did:
shaomq & mingqiEdit on GitHub
cljdoc is a website building & hosting documentation for Clojure/Script libraries
× close