Int32.to_int on a 64 bit machine

Hi everyone,

I have a little problem with this function from the library Int32 .
When i transform my Int32 to an integer, i obtain a 64 bit integer (normal because my arch is 64 bits).
But the non-relevant bits are all set to one (it seems) in a manner that a value such as
0xfeedface becomes 0x7FFFFFFFfeedface after using the function.

To reproduce the bug you need to :

open Format;;
let f  = open_in_bin "some_binary_file";;
let buff = Bytes.init 4 (fun i -> '\x00');;
input f buff 0 4;;
let i = Bytes.get_int32_ne buff 0;;
printf "%X" (Int32.to_int i);;

The same bug happens if you try to read an int from the in_channel using input_binary_in.

Thanks for the help !

this is because 0xfeedface as an int32 is a negative number; the representation of negative numbers starts with a bunch of ones

ocaml# Int32.to_int 0xfeedfacel;;
- : int = -17958194
ocaml# Int32.to_int 0xeedfacel;;
- : int = 250477262
ocaml# Int32.to_int 0xfeedfacel |> Printf.printf "%X\n%!";;
- : unit = ()
ocaml# Int32.to_int 0xeedfacel |> Printf.printf "%X\n%!";;
- : unit = ()
ocaml# Int64.to_int 0xfeedfaceL |> Printf.printf "%X\n%!";;
- : unit = ()