DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 5 of 5
  1. #1
    Join Date
    Feb 2007

    how to convert binary string to int?


    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. #2
    Join Date
    Jul 2005
    the Netherlands
    String binaryString = "1001";
    int base = 2;
    int decimal = Integer.parseInt(binaryString, base);

  3. #3
    Join Date
    Feb 2007
    hey, thanx alot man.

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

  4. #4
    Join Date
    Jul 2005
    the Netherlands
    Quote 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. #5
    Join Date
    Mar 2007
    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

Similar Threads

  1. No
    By chintucs in forum Java
    Replies: 2
    Last Post: 01-21-2007, 04:08 PM
  2. KeyListener not responding
    By lordanki in forum Java
    Replies: 3
    Last Post: 04-04-2006, 10:13 AM
  3. Replies: 3
    Last Post: 10-02-2005, 11:57 PM
  4. INSERT for multiple tables with IDENTITY
    By Kay in forum Database
    Replies: 1
    Last Post: 07-04-2001, 05:18 AM
  5. Trying to print a PDF File from VB
    By Kunal Sharma in forum VB Classic
    Replies: 2
    Last Post: 04-25-2000, 03:45 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
Latest Articles
Questions? Contact us.
Web Development
Latest Tips
Open Source

   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center