# Print bytes as hexadecimal

**URL:** <https://discuss.ocaml.org/t/print-bytes-as-hexadecimal/4814>\
**Category:** Learning\
**Created:** [December 4, 2019, 5:01pm UTC](https://discuss.ocaml.org/t/print-bytes-as-hexadecimal/4814 "2019-12-04T17:01:35Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Cjen1](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/cjen1/32/1731_2.png) [@Cjen1](https://discuss.ocaml.org/u/Cjen1)\
**Post date:** [December 4, 2019, 5:01pm UTC](https://discuss.ocaml.org/t/print-bytes-as-hexadecimal/4814/1 "2019-12-04T17:01:35Z")

</div>

I’m trying to find a function to convert a bytes into hexadecimal. This is in order to manually compare a tcp packet with one captured via wireshark.

Checking the interface on `Bytes` it doesn’t look like one exists, however I am most likely mistaken.

---

<div class="post-metadata">

**Author:** ![fangyi-zhou](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/fangyi-zhou/32/1887_2.png) [@fangyi-zhou](https://discuss.ocaml.org/u/fangyi-zhou)\
**Post date:** [December 4, 2019, 5:25pm UTC](https://discuss.ocaml.org/t/print-bytes-as-hexadecimal/4814/2 "2019-12-04T17:25:57Z")

</div>

[https://opam.ocaml.org/packages/hex/](https://opam.ocaml.org/packages/hex/)  
This package?

---

<div class="post-metadata">

**Author:** ![Chet\_Murthy](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/chet_murthy/32/1501_2.png) [@Chet\_Murthy](https://discuss.ocaml.org/u/Chet_Murthy)\
**Post date:** [December 5, 2019, 12:30am UTC](https://discuss.ocaml.org/t/print-bytes-as-hexadecimal/4814/3 "2019-12-05T00:30:37Z")

</div>

Like this?

```auto
        OCaml version 4.06.1

# Printf.sprintf "0x%02x" 33 ;;
- : string = "0x21"
# Printf.sprintf "%02x" 255 ;;
- : string = "ff"
# 

```

---

<div class="post-metadata">

**Author:** ![dinosaure](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/dinosaure/32/16_2.png) [@dinosaure](https://discuss.ocaml.org/u/dinosaure)\
**Post date:** [December 5, 2019, 11:41pm UTC](https://discuss.ocaml.org/t/print-bytes-as-hexadecimal/4814/4 "2019-12-05T23:41:11Z")

</div>

An other project is [`hxd`](https://github.com/dinosaure/hxd) available on OPAM which wants to mimic `xxd` and adds colors.

I use it to debug some Git flows with `tcpdump` and it gives me a way to produce regression tests then when it can generate Caml code.

---

<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:** [December 6, 2019, 12:55am UTC](https://discuss.ocaml.org/t/print-bytes-as-hexadecimal/4814/5 "2019-12-06T00:55:37Z")

</div>

Note that @pqwy recently contributed combinators in `Fmt` for hexdumping. See [here](https://erratique.ch/software/fmt/doc/Fmt/index.html#binary).
