Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Binary serialization of OCaml numeric types to and from strings

branch: master

Fetching latest commit…

Octocat-spinner-32-eaf2f5

Cannot retrieve the latest commit at this time

Octocat-spinner-32 src
Octocat-spinner-32 .gitignore Initial commit -- imported from Core 0.7 August 09, 2011
Octocat-spinner-32 LICENSE
Octocat-spinner-32 Makefile
Octocat-spinner-32 README.md
Octocat-spinner-32 _oasis
Octocat-spinner-32 _tags
Octocat-spinner-32 configure Switched to Kaputt-powered tests October 03, 2011
Octocat-spinner-32 myocamlbuild.ml
Octocat-spinner-32 setup.ml
Octocat-spinner-32 tests.ml
README.md

ocaml-binary

Binary serialization of OCaml numeric types to and from strings. Adapted from the Jane Street Capital Core, licensed as LGPL2.1 with OCaml linking exception.

Installation

Just the usual OASIS routines -- follow the white rabbit:

$ ocaml setup.ml -configure
$ ocaml setup.ml -build
$ ocaml setup.ml -install

Example

All Binary functions are labled and have a uniform interface; packing functions are named as pack_signed_<bits> where bits is one of [8; 16; 32; 64] and unpacking -- unpack_signed_<bits>:

# let buf = String.create 4;;
val buf : string = "\000\000\000\000"
# Binary.pack_signed_16 ~byte_order:`Big_endian ~pos:0 ~buf 42;;
- : unit = ()
# buf;;
- : string = "\000*\000\000"
# Binary.unpack_signed_16 ~byte_order:`Big_endian ~pos:0 ~buf;;
- : int = 42

However there are two exceptions worth mentioning -- both of the functions bellow extract native 32-bit and 64-bit integers, the former should only be used on x86_64 platforms and the latter on x86:

# let buf = String.create 4;;
val buf : string = "\000\000\000\000"
# Binary.pack_signed_32 ~byte_order:`Little_endian ~pos:0 ~buf 42l;;
- : unit = ()
# Binary.unpack_signed_32 ~byte_order:`Little_endian ~pos:0 ~buf;;
- : int32 = 42l
Something went wrong with that request. Please try again.