This week's giveaway is in the EJB and other Java EE Technologies forum.
We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line!
See this thread for details.
The moose likes Java in General and the fly likes Java and 2's Complement Conversion Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Java and 2 Watch "Java and 2 New topic
Author

Java and 2's Complement Conversion

D Lizzle
Greenhorn

Joined: Aug 18, 2010
Posts: 3
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

Joined: Sep 07, 2004
Posts: 781

<Removed badly thought out reply/>


Retired horse trader.
 Note: double-underline links may be advertisements automatically added by this site and are probably not endorsed by me.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 10908
    
  12

I'd try looking at the java API, and the Integer class, to see if there is some method that would parse a string


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
D Lizzle
Greenhorn

Joined: Aug 18, 2010
Posts: 3
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

Joined: Oct 25, 2008
Posts: 2700

D Lizzle wrote:Integer.parseInt works for only positive numbers.
And with that information you're not able to create something?


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
D Lizzle
Greenhorn

Joined: Aug 18, 2010
Posts: 3
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

Joined: Oct 13, 2005
Posts: 36468
    
  16
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

Joined: Oct 27, 2005
Posts: 19541
    
  16

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?


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36468
    
  16
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

Joined: Oct 27, 2005
Posts: 19541
    
  16

I'd prefer my suggestion for a broader type, then using the cast to truncate it.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Java and 2's Complement Conversion
 
Similar Threads
Math class method abs()
Getting wrong answers for conversion
"int" variable value calculation?
Binary in Java
Binary to decimal converter