# Js\_of\_ocaml: Bytes.t \<-\> Uint8Array

**URL:** <https://discuss.ocaml.org/t/js-of-ocaml-bytes-t-uint8array/12715>\
**Category:** Learning\
**Created:** [July 29, 2023, 4:12am UTC](https://discuss.ocaml.org/t/js-of-ocaml-bytes-t-uint8array/12715 "2023-07-29T04:12:03Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![zeroexcuses](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/zeroexcuses/32/1692_2.png) [@zeroexcuses](https://discuss.ocaml.org/u/zeroexcuses)\
**Post date:** [July 29, 2023, 4:12am UTC](https://discuss.ocaml.org/t/js-of-ocaml-bytes-t-uint8array/12715/1 "2023-07-29T04:12:03Z")

</div>

How do I do conversions of `Bytes.t <-> Uint8Array`. I know there is Bytes.to\_string , but my encoded bytes is arbitrary `Vec<u8>`, not necessairly a valid utf string.

Is the way to do the conversion to go through OCaml string, or is there some other more direct way ?

Thanks! (I tried grepping for `Bytes.t` in the js\_of\_ocaml source but not finding anything useful).

---

<div class="post-metadata">

**Author:** ![zeroexcuses](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/zeroexcuses/32/1692_2.png) [@zeroexcuses](https://discuss.ocaml.org/u/zeroexcuses)\
**Post date:** [July 29, 2023, 8:48am UTC](https://discuss.ocaml.org/t/js-of-ocaml-bytes-t-uint8array/12715/2 "2023-07-29T08:48:56Z")

</div>

I got something that compiles. It goes:

```auto
Bytes.t <-> string <-> Bigstring.t (Bigarray) <-> JS uint8array

```

This seems awfully redundant. Is there a cleaner solution ?

---

<div class="post-metadata">

**Author:** ![hhugo](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/hhugo/32/1143_2.png) [@hhugo](https://discuss.ocaml.org/u/hhugo)\
**Post date:** [July 29, 2023, 3:20pm UTC](https://discuss.ocaml.org/t/js-of-ocaml-bytes-t-uint8array/12715/3 "2023-07-29T15:20:44Z")

</div>

Create the typed array and set it inside a `Bytes.iter`

---

<div class="post-metadata">

**Author:** ![zeroexcuses](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/zeroexcuses/32/1692_2.png) [@zeroexcuses](https://discuss.ocaml.org/u/zeroexcuses)\
**Post date:** [July 29, 2023, 4:33pm UTC](https://discuss.ocaml.org/t/js-of-ocaml-bytes-t-uint8array/12715/4 "2023-07-29T16:33:04Z")

</div>

Is this a function call per every byte of the Bytes.t ?

I understand C’s memcpy does the same thing, but to be doing/looping this in JS; this sounds awfully slow.

---

<div class="post-metadata">

**Author:** ![zeroexcuses](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/zeroexcuses/32/1692_2.png) [@zeroexcuses](https://discuss.ocaml.org/u/zeroexcuses)\
**Post date:** [July 29, 2023, 5:08pm UTC](https://discuss.ocaml.org/t/js-of-ocaml-bytes-t-uint8array/12715/5 "2023-07-29T17:08:59Z")

</div>

I guess the other side of the argument is – of the 3 conversions I’m currently doing, atleast one is probably iterating in JS and copying byte by byte, so I might as well as just implement it myself and avoid the 3 layers of indirection.
