# Interpret bytes as packed binary data

**URL:** <https://discuss.ocaml.org/t/interpret-bytes-as-packed-binary-data/13292>\
**Category:** Learning\
**Created:** [October 22, 2023, 5:21pm UTC](https://discuss.ocaml.org/t/interpret-bytes-as-packed-binary-data/13292 "2023-10-22T17:21:47Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![vivanov](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/vivanov/32/4455_2.png) [@vivanov](https://discuss.ocaml.org/u/vivanov)\
**Post date:** [October 22, 2023, 5:21pm UTC](https://discuss.ocaml.org/t/interpret-bytes-as-packed-binary-data/13292/1 "2023-10-22T17:21:47Z")

</div>

Hey, I am looking to interpret bytes as packed binary data. Like `struct` in `python`. I found `cstruct` in `ocaml`. Are there alternative libraries?

---

<div class="post-metadata">

**Author:** ![dbuenzli](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/dbuenzli/32/18_2.png) [@dbuenzli](https://discuss.ocaml.org/u/dbuenzli)\
**Post date:** [October 22, 2023, 6:03pm UTC](https://discuss.ocaml.org/t/interpret-bytes-as-packed-binary-data/13292/2 "2023-10-22T18:03:25Z")

</div>

That’s a broad request and I don’t know what `struct` gives you in python.

In any case nowadays the `stdlib` has sized integer [getters](https://v2.ocaml.org/api/String.html#1_Binarydecodingofintegers) on `string` and [getter and setters](https://v2.ocaml.org/api/Bytes.html#1_Binaryencodingdecodingofintegers) on `bytes`.

Depending on what you need to do – decode a binary format, or hide access to packed bytes behind an abstract type, that may be entirely sufficient.

---

<div class="post-metadata">

**Author:** ![yawaramin](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/yawaramin/32/3384_2.png) [@yawaramin](https://discuss.ocaml.org/u/yawaramin)\
**Post date:** [October 22, 2023, 7:16pm UTC](https://discuss.ocaml.org/t/interpret-bytes-as-packed-binary-data/13292/3 "2023-10-22T19:16:56Z")

</div>

If you need to parse binary data, check out [https://bitstring.software/](https://bitstring.software/)

---

<div class="post-metadata">

**Author:** ![lindig](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/lindig/32/532_2.png) [@lindig](https://discuss.ocaml.org/u/lindig)\
**Post date:** [October 23, 2023, 9:00am UTC](https://discuss.ocaml.org/t/interpret-bytes-as-packed-binary-data/13292/4 "2023-10-23T09:00:28Z")

</div>

I have used [Angstrom](https://opam.ocaml.org/packages/angstrom/) to parse binary data: [fit](https://opam.ocaml.org/packages/fit) parses the binary FIT format invented by Garmin for watches and other sensors that produce data for fitness activities. Angstrom can be used to parse both binary and textual data. If performance is important, it might not be the most efficient because it depends on back tracking.

Below is the code that parses the header of a FIT file.

```auto
let header =
    any_int8 >>= function
    | (12 | 14) as size ->
        any_int8 >>= fun protocol ->
        LE.any_int16 >>= fun profile ->
        LE.any_int32 >>= fun length ->
        string ".FIT"
        *> (if size = 14 then advance 2 (* skip CRC *) else advance 0)
        *> return { protocol; profile; length = Int32.to_int length }
    | n -> fail "found unexpected header of size %d" n

```
