how to convert binary string to int?

 DevX Home Today's Headlines   Articles Archive   Tip Bank   Forums

# Thread: how to convert binary string to int?

1. ## how to convert binary string to int?

Hi,

I need to know how you convert a binary string into its integer variable type equivelant.

For example,
String s="1001" (which is actually the number 9 in binary base 2)

I want to know how to convert it into an int or long variable type.
Is there a method that will take the binary string ie. "Integer.convertBinaryString()" that will take my binary string and convert it into the Base 10 integer equivelant?

Ive heard of the Integer.Decode() method, but I think it only works for converting base 8 and base 16 strings into the integer equivelant. I dont think it converts base 2 strings into int. Does anyone know how to convert a base 2 number in string form into the base 10 integer equivelant?

2. Registered User
Join Date
Jul 2005
Location
the Netherlands
Posts
128
Code:
```String binaryString = "1001";
int base = 2;
int decimal = Integer.parseInt(binaryString, base);```

3. hey, thanx alot man.

I must have some how over looked it in the java API docs if it was there.

4. Registered User
Join Date
Jul 2005
Location
the Netherlands
Posts
128
Originally Posted by bob2000
hey, thanx alot man.

I must have some how over looked it in the java API docs if it was there.
They call the base "the radix" in the API docs.

5. Registered User
Join Date
Mar 2007
Posts
1
Hello Everybody!!

Prometheuzz.... I thik U cn help me vid my problem.. I have an array of String type...
each index is storing a 32-bit binary String...
I wish to convert it into an Integer array.. I have to use these integer values as pixel values and create an image using these.. M Badly Stuck Up... A binary String like "00001111" when converted to integer returns just "1111" .. How can I conserve these zero's...
as of now I have used both the above mentioned statements.. guide me thru...

Thank You

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•

 FAQ Latest Articles Java .NET XML Database Enterprise