How to convert numbers from hex, oct, decimal and binary to OCaml?

What is the general way to convert numbers from hex, oct and binary to OCaml?

For example, how to convert hex 4294967276 (FFFFFFFC) to decimal?

Thank!

Nick

+5
source share
1 answer

It is not clear what you are asking. In fact, numbers are just numbers. They have no base (ignoring the hardware representation). The time when you have a base is the conversion of numbers to and from strings. To interpret a string as a decimal integer, you can use int_of_string. For other bases you can use Scanf.sscanf. To convert a number to a decimal string, you can use string_of_int. For other bases you can use Printf.sprintf.

# int_of_string "345";;
- : int = 345
# Scanf.sscanf "FC" "%x" (fun x -> x);;
- : int = 252
# string_of_int 345;;
- : string = "345"
# Printf.sprintf "%X" 252;;
- : string = "FC"
# 

, Scanf.sscanf . OCaml.

barti_ddu, , int_of_string 4 :

# int_of_string "0xFC";;
- : int = 252
# int_of_string "0o374";;
- : int = 252
# int_of_string "0b11111100";;
- : int = 252

(, , , .)

+9

All Articles