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
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 *)
$ 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?