• Post Reply Bookmark Topic Watch Topic
  • New Topic

decimal to binary  RSS feed

 
nirjari patel
Ranch Hand
Posts: 386
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there a any ready made utility available in any java package which can convert decimal to binary ?

If yes, which one and in which package ?

Thanks
 
Paul Clapham
Sheriff
Posts: 22823
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, there are static methods in the Integer class for that sort of thing.
 
nirjari patel
Ranch Hand
Posts: 386
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks,

I found that toBinaryString() to convert a decimal to binary. I did not find any method for converting a binary to decimal.

Which method converts a binary value to decimal and in which package is that ?

Thanks
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
is your input a String? could you use parseInt(String s, int radix), from the Integer class?
 
nirjari patel
Ranch Hand
Posts: 386
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am talking about integer. If I want to see the decimal value of a binary integer, hiw can I check that ? Whats the available method for that ?
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do you mean by a 'binary integer'? Java has an Integer class, and an int primitive. How the values are stored is not relevant to how they are displayed.

So let's back up a little. What exactly do you have, and what exactly do you need to do? Can you provide some code samples to show us?
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How exactly do you have a binary integer? There's no binary integer type. Hence Fred assuming that what you have was actually a String containing the binary representation of an integer.

If that's what you've got, his method will work. If not, what exactly do you have?

Edit: snap! One second!
 
nirjari patel
Ranch Hand
Posts: 386
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What I meant is , lets say I have a binary value of 1100000000011000000000000000011.

Now I want to find out decimal value of this binary value, how can I do that ? Is there a a method available for this conversion ?



When I run this code, I am getting foillowing result with an exception


Thanks
 
Campbell Ritchie
Marshal
Posts: 56530
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is because you have got an instance field and didn't write a constructor to initialise it.
 
nirjari patel
Ranch Hand
Posts: 386
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am sorry. That code has been rewritten. I have taken out that instance field.

When the binary sring starts with 0 (ie for posiive integers), its working and I am getting the decimal value of binary, but for negative integers (starting with 1), I am getting an exception.

Please let me know what wrong in here.

Thanks
 
Rob Spoor
Sheriff
Posts: 21133
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Integer.parseInt doesn't work with negative binary numbers. So what you need to do is manually check the first character; if it's a 0 then you perform a normal parseInt. If it's a 1 you have to work some magic:
The 1 << -1 part is basically the 10000000000000000000000000000000 part. The Integer.parseInt(con2.substring(1), 2) is the 01000000000000000000000000000011 part. So you now have two numbers. Using | you get the bitwise or which is 11000000000000000000000000000011 - the number you want.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!