# OCaml equivalent of Javascript Uint8Array literals?

**URL:** <https://discuss.ocaml.org/t/ocaml-equivalent-of-javascript-uint8array-literals/10528>\
**Category:** Learning\
**Created:** [September 25, 2022, 7:48pm UTC](https://discuss.ocaml.org/t/ocaml-equivalent-of-javascript-uint8array-literals/10528 "2022-09-25T19:48:17Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![dangdennis](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/dangdennis/32/2594_2.png) [@dangdennis](https://discuss.ocaml.org/u/dangdennis)\
**Post date:** [September 25, 2022, 7:48pm UTC](https://discuss.ocaml.org/t/ocaml-equivalent-of-javascript-uint8array-literals/10528/1 "2022-09-25T19:48:17Z")

</div>

In javascript, I can get the hex string of a Uint8Array by wrapping it in a Buffer(int\_array).toString(‘hex’).

I’m looking for an OCaml equivalent where I can instantiate a byte array like below. Is there a Bytes method that does what `new Uint8Array` does directly? It looks like I have to init a bytes array of a specific length, and then iterate through it to set each value.

```auto
const publicKeyArr = new Uint8Array([
      247, 115, 126, 69, 180, 61, 206, 136, 176, 58, 14, 251, 163, 119, 183, 51, 220, 33, 166, 85,
      89, 253, 169, 240, 21, 195, 83, 35, 55, 135, 118, 25, 39, 83, 236, 186, 20, 8, 154, 3, 37,
      183, 120, 143, 237, 114, 101, 86, 143, 148, 63, 227, 245, 95, 115, 138, 121, 34, 79, 69, 21,
      209, 61, 76,
    ]) 

const buffHexString = new Buffer(publicKeyArr).toString('hex') // desired state

```

My end goal is to produce a string that is hashed by keccak256 to eventually produce an ethereum address from an ECDSA public key.

---

<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:** [September 25, 2022, 7:53pm UTC](https://discuss.ocaml.org/t/ocaml-equivalent-of-javascript-uint8array-literals/10528/2 "2022-09-25T19:53:58Z")

</div>

Not sure exactly which types you want but:

```auto
let pub_key : string = "\247\115\…" 
let pub_key : Bytes.t = Bytes.unsafe_of_string "\247\115\…" 

```

---

<div class="post-metadata">

**Author:** ![dangdennis](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/dangdennis/32/2594_2.png) [@dangdennis](https://discuss.ocaml.org/u/dangdennis)\
**Post date:** [September 25, 2022, 7:57pm UTC](https://discuss.ocaml.org/t/ocaml-equivalent-of-javascript-uint8array-literals/10528/3 "2022-09-25T19:57:25Z")

</div>

I’ll try this. I don’t quite know what type I want yet either. 😄

Given your input, it looks like I can use [ez\_hash’s `hash_bytes`](https://github.com/OCamlPro/ocaml-solidity/blob/f7265bd39436c4e154a8b9559e42514399a17c32/src/ez_hash/ezHash.ml#L188). The input type is `bytes`.

---

<div class="post-metadata">

**Author:** ![alan](https://avatars.discourse-cdn.com/v4/letter/a/a87d85/32.png) [@alan](https://discuss.ocaml.org/u/alan)\
**Post date:** [September 25, 2022, 7:58pm UTC](https://discuss.ocaml.org/t/ocaml-equivalent-of-javascript-uint8array-literals/10528/4 "2022-09-25T19:58:42Z")

</div>

> [@dangdennis](#):
>
> It looks like I have to init a bytes array of a specific length, and then iterate through it to set each value.

You might be looking for [OCaml library : Buffer](https://v2.ocaml.org/releases/4.14/api/Buffer.html)

---

<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:** [September 25, 2022, 7:59pm UTC](https://discuss.ocaml.org/t/ocaml-equivalent-of-javascript-uint8array-literals/10528/5 "2022-09-25T19:59:21Z")

</div>

Well use a `string` and [`hash`](https://github.com/OCamlPro/ocaml-solidity/blob/f7265bd39436c4e154a8b9559e42514399a17c32/src/ez_hash/ezHash.ml#L178) instead. That will avoid a needless scary `Bytes.unsafe_of_string`.

Also you can escape using [hex (or octal)](https://v2.ocaml.org/manual/lex.html#escape-sequence) if you need to.

---

<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:** [September 25, 2022, 9:16pm UTC](https://discuss.ocaml.org/t/ocaml-equivalent-of-javascript-uint8array-literals/10528/6 "2022-09-25T21:16:44Z")

</div>

It seems like you’re pointing at two separate problems:

1. no nice syntax for writing down non-ASCII string constants

2. Once you _have_ a string, applying various operations to it, that are today only supplied on Bytes.

I’d think that those are separate: if you have a Buffer type that only takes bytes, then passing it a constant of any kind isn’t going to work – b/c bytes are mutable, right? So really, what’s needed, is better support for treating string like bytes in more cases. Heh [not that I mean to beat on that dead horse] more ad-hoc polymorphism.

And then, separately, maybe you want better syntax. So one could imagine a teeny-tiny PPX extension (as an expression), viz.

`[%bytes (1,2,3,4,5,255,0xf3)]`

that meant … well, the obvious thing: a bytes wiith the specified octet content.

Would that help?

---

<div class="post-metadata">

**Author:** ![dangdennis](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/dangdennis/32/2594_2.png) [@dangdennis](https://discuss.ocaml.org/u/dangdennis)\
**Post date:** [September 26, 2022, 3:46am UTC](https://discuss.ocaml.org/t/ocaml-equivalent-of-javascript-uint8array-literals/10528/7 "2022-09-26T03:46:52Z")

</div>

Thanks everyone. I was able to use `Buffer` to effectively mirror what I could do with JavaScript’s Uint8Array.

```auto
  let int_arr = [....]
  let b = Buffer.create 32 in
  let () = List.iter (fun i -> Buffer.add_int8 b i) int_arr in
  Buffer.to_bytes b |> EzHex.Hex.encode_bytes |> print_endline;

```

Looking back, yes my original question hits a couple hurdles. At first I wasn’t sure how a js string compared to an ocaml string, so wondered how I’d be able to convert them to their binary form (`bytes type`). But yay a js uint8 is an ocaml uint8. This meant I was able to create the Buffer as expected.

Onto the next!
