Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Java and 2's Complement Conversion

 
D Lizzle
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Given the following Strings that represent binary numbers in two's complement:


Does a method exist in Java that will convert those strings to their decimal representation?

I have created a method that does the conversion correctly, but it seems like I am re-inventing the wheel since I'm sure it is common to do a two's complement binary number to decimal conversion. It involves looking at the first character for the sign, and if it is '1' (negative), flip each bit, call Integer.parseInt(bitFlippedValue, 2), add 1 and multiply by -1, else if the first character is '0', simply call Integer.parseInt(value, 2).

Thanks in advance.
D
 
James Sabre
Ranch Hand
Posts: 781
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
<Removed badly thought out reply/>
 
fred rosenberger
lowercase baba
Bartender
Posts: 12122
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'd try looking at the java API, and the Integer class, to see if there is some method that would parse a string
 
D Lizzle
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred rosenberger wrote:I'd try looking at the java API, and the Integer class, to see if there is some method that would parse a string

That's the first place I looked. I couldn't find a method that would convert a 32 bit two's complement binary number as a String into an integer value. Integer.parseInt works for only positive numbers.
 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
D Lizzle wrote:Integer.parseInt works for only positive numbers.
And with that information you're not able to create something?
 
D Lizzle
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wouter Oet wrote:
D Lizzle wrote:Integer.parseInt works for only positive numbers.
And with that information you're not able to create something?


Read my original post.
 
Campbell Ritchie
Sheriff
Posts: 48917
58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
D Lizzle wrote: . . . Read my original post.
Yes, there is the problem that a String with 32 0/1 characters cannot be parsed by that method
campbell@queeg:~/java$ java IntegerParseDemo 0
0 parses to 0
campbell@queeg:~/java$ java IntegerParseDemo 0111111111111111
0111111111111111 parses to 32767
campbell@queeg:~/java$ java IntegerParseDemo 011111111111111111111111
011111111111111111111111 parses to 8388607
campbell@queeg:~/java$ java IntegerParseDemo 01111111111111111111111111111111
01111111111111111111111111111111 parses to 2147483647
campbell@queeg:~/java$ java IntegerParseDemo 11111111111111111111111111111111
Exception in thread "main" java.lang.NumberFormatException: For input string: "11111111111111111111111111111111"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
at java.lang.Integer.parseInt(Integer.java:461)
at IntegerParseDemo.main(IntegerParseDemo.java:5)
You can see where I go from 0 and 31 1s to 32 1s and get the NumberFormatException.
 
Rob Spoor
Sheriff
Pie
Posts: 20527
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, so 11111111111111111111111111111111 does not fit in an int. What's the numeric type one step above int in size? And what happens if you cast something from that type to int?
 
Campbell Ritchie
Sheriff
Posts: 48917
58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
D Lizzle wrote:. . . It involves looking at the first character for the sign, and if it is '1' . . .
. . . parse the other 31 digits, then add -2147483648. Or better still, the constant value in the java.lang.Integer class.

Otherwise, if Rob Prime suggests anything he is always correct.
 
Rob Spoor
Sheriff
Pie
Posts: 20527
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'd prefer my suggestion for a broader type, then using the cast to truncate it.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic