# Reversed byte order when reading data sequentially with input\_byte

**URL:** <https://discuss.ocaml.org/t/reversed-byte-order-when-reading-data-sequentially-with-input-byte/17806>\
**Category:** Learning\
**Tags:** stdlib, bytes\
**Created:** [February 13, 2026, 3:24am UTC](https://discuss.ocaml.org/t/reversed-byte-order-when-reading-data-sequentially-with-input-byte/17806 "2026-02-13T03:24:53Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![mhitza](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/mhitza/32/5477_2.png) [@mhitza](https://discuss.ocaml.org/u/mhitza)\
**Post date:** [February 13, 2026, 3:24am UTC](https://discuss.ocaml.org/t/reversed-byte-order-when-reading-data-sequentially-with-input-byte/17806/1 "2026-02-13T03:24:54Z")

</div>

I’m trying to read bytes in order from an input channel. Using consecutive byte reads in an array initialization results in data stored in reverse order.

Example

```ocaml
type elf_bytes = { ident : int array; }                                                                                  
                                                                                                                         
let () =                                                                                                                 
  let ic = open_in_bin "/proc/self/exe" in
  let res = really_input_string ic 16 in
  String.iter (fun x -> Printf.printf "%d " (int_of_char x)) res;
  print_newline ();
  (* prints 127 69 76 70 2 1 1 0 0 0 0 0 0 0 0 0 <- expected *)
  seek_in ic 0;      
  let hdr = { ident =
    [| input_byte ic; input_byte ic; input_byte ic; input_byte ic;
       input_byte ic; input_byte ic; input_byte ic; input_byte ic;
       input_byte ic; input_byte ic; input_byte ic; input_byte ic;
       input_byte ic; input_byte ic; input_byte ic; input_byte ic |]
  } in
  Array.iter (fun x -> Printf.printf "%d " x) hdr.ident;
  print_newline ();
  (* prints 0 0 0 0 0 0 0 0 0 1 1 2 70 76 69 127 <- actual *)

```

```shell
$ hexdump /proc/self/exe | head -1
0000000 457f 464c 0102 0001 0000 0000 0000 0000
        ^ ^ ^ ^
        69 | 70 |
          127 76

```

Which would be the appropriate way for me to read (using the standard lib) bytes into these initialization array records?

---

<div class="post-metadata">

**Author:** ![jrfondren](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/jrfondren/32/3871_2.png) [@jrfondren](https://discuss.ocaml.org/u/jrfondren)\
**Post date:** [February 13, 2026, 4:27am UTC](https://discuss.ocaml.org/t/reversed-byte-order-when-reading-data-sequentially-with-input-byte/17806/2 "2026-02-13T04:27:51Z")

</div>

Evaluation order is probably unspecified, as with constructor arguments:

```ocaml
# [|print_endline "1"; print_endline "2"|];;
2
1
- : unit array = [|(); ()|]

```

An easy option for this case is

```ocaml
Array.init 16 (fun _ -> input_byte ic)

```

In general you can fix evaluation order with `let ... in ...`as you’re already doing to construct `hdr` before iterating on it.

---

<div class="post-metadata">

**Author:** ![mhitza](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/mhitza/32/5477_2.png) [@mhitza](https://discuss.ocaml.org/u/mhitza)\
**Post date:** [February 13, 2026, 4:34am UTC](https://discuss.ocaml.org/t/reversed-byte-order-when-reading-data-sequentially-with-input-byte/17806/3 "2026-02-13T04:34:28Z")

</div>

Thank you. I wasn’t aware of the fact that order is unspecified for this language construct. Assumed “imperative” evaluation order in the absence of laziness and such.

---

<div class="post-metadata">

**Author:** ![nojb](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/nojb/32/519_2.png) [@nojb](https://discuss.ocaml.org/u/nojb)\
**Post date:** [February 13, 2026, 6:31am UTC](https://discuss.ocaml.org/t/reversed-byte-order-when-reading-data-sequentially-with-input-byte/17806/4 "2026-02-13T06:31:10Z")

</div>

> [@mhitza](#):
>
> I wasn’t aware of the fact that order is unspecified for this language construct. Assumed “imperative” evaluation order in the absence of laziness and such.

Indeed, evaluation order is unspecified. In practice, it is right-to-left everywhere (this is the order that is most natural for the implementation of currying), but it is better not to depend on this.

Cheers,  
Nicolas

---

<div class="post-metadata">

**Author:** ![otini](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/otini/32/3533_2.png) [@otini](https://discuss.ocaml.org/u/otini)\
**Post date:** [February 13, 2026, 9:42am UTC](https://discuss.ocaml.org/t/reversed-byte-order-when-reading-data-sequentially-with-input-byte/17806/5 "2026-02-13T09:42:07Z")

</div>

It’s the case in bytecode, but I had the notion that in native code, it’s left-to-right. Isn’t it?

---

<div class="post-metadata">

**Author:** ![nojb](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/nojb/32/519_2.png) [@nojb](https://discuss.ocaml.org/u/nojb)\
**Post date:** [February 13, 2026, 9:59am UTC](https://discuss.ocaml.org/t/reversed-byte-order-when-reading-data-sequentially-with-input-byte/17806/6 "2026-02-13T09:59:39Z")

</div>

No, I don’t think so. More generally, evaluation order differences between byte- and native-code compilation should be considered a bug (see eg [Evaluation order: ocamlopt difference on application of mutable field · Issue #13874 · ocaml/ocaml · GitHub](https://github.com/ocaml/ocaml/issues/13874) and [Ensure right-to-left evaluation of arguments in cmm\_helpers by gretay-js · Pull Request #10732 · ocaml/ocaml · GitHub](https://github.com/ocaml/ocaml/pull/10732)).

Cheers,  
Nicolas

---

<div class="post-metadata">

**Author:** ![otini](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/otini/32/3533_2.png) [@otini](https://discuss.ocaml.org/u/otini)\
**Post date:** [February 13, 2026, 10:04am UTC](https://discuss.ocaml.org/t/reversed-byte-order-when-reading-data-sequentially-with-input-byte/17806/7 "2026-02-13T10:04:46Z")

</div>

It was a misconception on my part then. Thanks!
