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

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

Thanks!

Nick

It's not clear what you're asking. Numbers are just numbers, really. They don't have a base (ignoring the hardware representation). The time you have a base is when you convert 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"
#
```

Granted, `Scanf.sscanf`

is fairly cumbersome to use. But I don't know of any other conversion functions in the OCaml Standard Library.

**Update**

As observed by barti_ddu, if you don't mind adding a prefix to your input string you can use `int_of_string`

for all 4 of the bases you mention:

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

(Or maybe your input already has the prefix, in which case this is a *much* simpler solution.)

Similar Questions

I need to convert hex to a decimal in VB.NET. Found several examples in C#, but when I tried to convert to VB.NET I was not successful. An example of a hexadecimal number that I am trying to convert i

How to convert from Hex string to ASCII string in JavaScript? Ex: 32343630 it will be 2460

I have a program written in Delphi is converting 11 to 0xB and 28 to 0x1c. I tried to convert 11 (decimal to Hex) in c# using this:- var deciValue01 = 11; var deciValue02 = 28; var deciValue03 = 13; S

Hi I need to convert a hex number to binary number in C++. It represents a 4 by 8 binary image. For example, the following hex number 0xc00e90 represent the image: 00000000 11000000 00001110 10010000

I must admit I don't remember much about HEX and so on from school (25 years ago). In any case, I have some values in decimal format which I need to convert into HEX. I am using Excel but I could writ

How do you convert decimal values to their hex equivalent in JavaScript?

All I really know is PHP and I used the decbin function etc, It was fairly easy to do. In this C++ program I want to do the same thing, just a simple number or string how would I do this?

I need to convert a hex string to binary form in objective-c, Could someone please guide me? For example if i have a hex string 7fefff78, i want to convert it to 1111111111011111111111101111000? BR, S

This is a basic question and probably very easy but I am really confused. I have an 8-bit hex number 0x9F and I need to interpret this number as an unsigned decimal number. Do I just convert that to

I am using Perl I got a string agn\u00e8s b How can I convert it to agnès b I have tried to use below code my $hex = agn\u00e8s b; $hex =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack(C,hex($1))/eg; pri

I'm wondering if there are built in classes for working with binary numbers and converting any number (in this case decimal) to a binary and vice-versa. For example if I want to check if the third bit

I have a hex string like this 529CD17C.This is corresponding to One date time 12/2/2013 06:29:16 PM .(ie in MM/dd/yyyy hh:mm:ss AM/PM).How can i do this in c# coding

Possible Duplicate: How do I convert hex string into signed integer? example:3A convert to 0x3A,thanks a lot!

How can I convert a hex ip (such as 42477f35), and get it to spit out the correct decimal ip (in the example before, the correct result is 66.71.127.53)? I'd like it to take the hex ip in as a string,

I have to do this exercise and am completely stumped as to how. I've constructed some FA's before, but using binary numbers. How can I do this but for even decimal numbers?

How do I convert $ 10.80 to decimal? By using regex?

Im trying to make a binary string into a decimal. It will terminate if -1 is entered. I am stuck with using an array. It was suggested to use: public static int binaryToDecimal (String binaryString) .

I'm trying to convert raw hex/binary data to different file types. #include <QByteArray> #include <QDebug> int main(int argc, char *argv[]) { QByteArray package; package.append( QByteArray

I'm using log4net for logging and my conversion pattern includes threadId in my output, but its only highest 2 digits and i am asked to convert it to hexadecimal but so far i found code C# to convert

I know that there are two function 'fix' and 'round' which can convert decimal to integer. However, I cant find a way to round off any decimal number to its next integer. Anyone know how this can be d

I have a long hex value stored as a NSString, something like: c12761787e93534f6c443be73be31312cbe343816c062a278f3818cb8363c701 How do I convert it back into a binary value stored as a char*

Hi I'm working on an assignment and got stuck on this part, how do I validate decimal numbers/numbers in shell? It can accept numbers but not decimal numbers. I want it to be able to accept both. This

This question already has an answer here: How to convert numbers between hexadecimal and decimal in C#? 13 answers I want to convert a string to a decimal then to a string and then to a decimal

This question already has an answer here: In Java, how do I convert a byte array to a string of hex digits while keeping leading zeros? 24 answers I have a binary string containing 160 digit. I

how to convert Decimal to binary and viceversa i am working on solaris10 platform can anybody help me with a command Decimal to Binary 4000000002-100000000000000000000000000010 Binary to decimal 10000

Possible Duplicate: Convert an integer to binary without using the built-in bin function how do I convert a decimal number into a binary list. For example, how do I change 8 into [1,0,0,0]

The most common way is to get the power of 2 for each non-zero position of the binary number, and then sum them up. This is not workable when the binary number is huge, say, 10000...0001 //1000000 po

VB2010. This is an offshoot of a prior question (Convert Hex Strings to Integers) I had but now have additional info and wanted to make this a separate post. I am tying to translate hex data from a le

I am trying to simply convert to Binary with recursion. I am having problems with the return statement. This compiles but give an overflow error when run. I don't know what to return (or if my stateme

I've been told this : Computers convert binary data into the hexadecimal (hex) number system because it is much less complex than converting data into decimal numbers. Why it is less complex?

I have written a program in C to convert a floating point number represented in binary (1101.11) into a decimal (13.75). However, I cannot seem to get the correct value out of the algorithm. What is

int rem, count = 0; long int n=0, b, i; count << Enter the Binary value to convert in Decimal = ; cin >> b; i = b; while (b > 0) { rem = b % 10; n = n + rem * pow(2, count); count++;

Today I was just playing around for basic conversions from one base to another. I goggled some code for converting from hex to octal, and I noticed that it mostly uses intermediate conversion to eithe

I have a file with hex numbers as follows: 00042980 00020000 00020000 00028000 00020008 00021000 01028000 00000000 00000000 In this same exact fashion. How do I read this file in binary in C++?

This is how to convert from decimal to binary: int dec = 42; string bin = Convert.ToString(dec, 2); How to convert binary to decimal if I have string bin = 0100101 int dec = ???

I want to convert a number to hex and store the result in a []byte up to length 4. Here's what I came up with, but it feels very roundabout. package main import ( encoding/hex fmt ) func main() {

How do I convert a decimal number to a hex string in Perl? For example, I would like to convert 2001 into 0x07 and 0xD1.

I have a question on data conversion from binary to decimal. Suppose I have a binary pattern like this: pattern<-do.call(expand.grid, replicate(5, 0:1, simplify=FALSE)) pattern Var1 Var2 Var3 Var4

i would like to convert hex string to binary stirng, example Hex 2 is 0010. Below is the code String HexToBinary(String Hex) { int i = Integer.parseInt(Hex); String Bin = Integer.toBinaryString(i); r

Good afternoon, How would you go about converting to a decimal string a binary string with more characters than bits in a language's largest integer type? In other words, supposing that you have the s

How do you truncate all numbers after a decimal in Python 3? For example truncate 3.444 to be just 3.

hi can any one please help me on how to convert from 10/21/2009 to Wed Oct 21 18:48:12 UTC+0530 2009 in my java class

how to convert a unicode string in java to hex string. for example : st = \u0645 to st = %D9%85 ?

To write a program to convert from decimal to negabinary I know that how to convert from decimal to binary by divided to 2 for each digits and pick the remainder. But I cannot figure out how to conver

Can anyone suggest me a way to multiply decimal numbers without using multiplication(*) sign. I know it looks like homework thing, but I just want to know how to achieve this. I have already done this

I am trying to store every hex value from 0000 to fffe into a file. How would I use fwrite or fprintf to place these values into a file? At the moment, I have a while loop that prints all the numbers

I have a hex file of the first 100 million digits of pi after the decimal, in a hex editor it looks like this: 14 15 92 65 35 89 79 32 etc. I need to convert this to the string (and eventually intege

I want to ask how can i convert from hex to decimal in Java ? I tried using parseInt by getting the number from the user in an edit text and view the result in a text view after splitting the long spa

HEX Article By this I mean, In a program if I write this: 1111 I mean 15. Likewise, if I write this: 0xF I also mean 15. I am not entirely sure how the process is carried out through the chip, but

How do you convert a hex code represented in a string to a byte and the reverse in Javascript? var conv = require('binstring'); var hexstring ='80'; var bytestring = conv(hexstring, {in:'hex', out:'ut