NtOrZor Posted September 29, 2004 Share Posted September 29, 2004 Is there a good way to convert a integer to a 2's complement binary and store the result as a String? In the Integer class there is a static method toBinaryString(int i) but it only handles "unsigned" integers. How do I do it with signed integers? Link to comment https://www.neowin.net/forum/topic/223959-javaconverting-integer-to-signed-binary/ Share on other sites More sharing options...
0 eccofresh Posted September 29, 2004 Share Posted September 29, 2004 try this: int i = 7; String s = Integer.toString(i,2); // the 2 is what converts it to binary System.out.println(s); Link to comment https://www.neowin.net/forum/topic/223959-javaconverting-integer-to-signed-binary/#findComment-584645049 Share on other sites More sharing options...
0 NtOrZor Posted September 29, 2004 Author Share Posted September 29, 2004 Quote String s = Integer.toString(i,2); // the 2 is what converts it to binarySystem.out.println(s); That still doesn't work because the function does not handle negative numbers. It instead puts a '"-" sign in front of the string. Link to comment https://www.neowin.net/forum/topic/223959-javaconverting-integer-to-signed-binary/#findComment-584645172 Share on other sites More sharing options...
0 ramesees Posted September 29, 2004 Share Posted September 29, 2004 When I learned to convert integers to their 2's complement notation, I was taught to reverse the bits then add 1. So could you do the following: 1) take an integer and convert it to bytes 2) go through each bit and reverse them (0->1 and 1->0) 3) add 1 to this "flipped bit" number 4) your result is that number in 2's complement notation Now I realise there are probably much easier methods available to do this, this is just my first thoughts on the subject Link to comment https://www.neowin.net/forum/topic/223959-javaconverting-integer-to-signed-binary/#findComment-584645483 Share on other sites More sharing options...
0 NtOrZor Posted September 29, 2004 Author Share Posted September 29, 2004 Yes I know how to convert to 2's complement, but I was just wondering if any one of the classes in the java library offer a easier solution then to code everything by myself. Link to comment https://www.neowin.net/forum/topic/223959-javaconverting-integer-to-signed-binary/#findComment-584646902 Share on other sites More sharing options...
0 Mouton Posted September 29, 2004 Share Posted September 29, 2004 Integer.toBinaryString() "For negative numbers it will return a 32 bit two's compliment representation of that number." Link to comment https://www.neowin.net/forum/topic/223959-javaconverting-integer-to-signed-binary/#findComment-584646967 Share on other sites More sharing options...
0 NtOrZor Posted September 30, 2004 Author Share Posted September 30, 2004 Quote Integer.toBinaryString()"For negative numbers it will return a 32 bit two's compliment representation of that number." Where does it say that? I'm looking at the java.sun.com API specification and this is what is said Quote Returns a string representation of the integer argument as an unsigned integer in base 2.The unsigned integer value is the argument plus 232 if the argument is negative; otherwise it is equal to the argument. This value is converted to a string of ASCII digits in binary (base 2) with no extra leading 0s. If the unsigned magnitude is zero, it is represented by a single zero character '0' ('\u0030'); otherwise, the first character of the representation of the unsigned magnitude will not be the zero character. The characters '0' ('\u0030') and '1' ('\u0031') are used as binary digits. Nowhere does it says it will return a 32 bit two's complement representation! Can you tell me where you got that? Link to comment https://www.neowin.net/forum/topic/223959-javaconverting-integer-to-signed-binary/#findComment-584648102 Share on other sites More sharing options...
0 Mouton Posted September 30, 2004 Share Posted September 30, 2004 Google Groups class Test{ public static void main(String args[]){ System.out.println(Integer.toBinaryString(1)); System.out.println(Integer.toBinaryString(-1)); System.out.println(Integer.toBinaryString(256)); System.out.println(Integer.toBinaryString(-256)); System.out.println(Integer.toBinaryString(0)); System.out.println(Integer.toBinaryString(31337)); System.out.println(Integer.toBinaryString(-31337)); } } C:\>javac Test.java C:\>java Test 1 11111111111111111111111111111111 100000000 11111111111111111111111100000000 111101001101001 11111111111111111000010110010111 C:\> As advertized, gives the 32-bits 2-complement when used with negative numbers. Link to comment https://www.neowin.net/forum/topic/223959-javaconverting-integer-to-signed-binary/#findComment-584648135 Share on other sites More sharing options...
0 NtOrZor Posted September 30, 2004 Author Share Posted September 30, 2004 Wow, it really works?! I'm just amazed that they would write it can't handle negative numbers on Sun's website when the function clearly does! Link to comment https://www.neowin.net/forum/topic/223959-javaconverting-integer-to-signed-binary/#findComment-584652534 Share on other sites More sharing options...
0 Mouton Posted October 1, 2004 Share Posted October 1, 2004 They just use different words to explain the 2-complement. "The unsigned integer value is the argument plus 2^32 if the argument is negative; otherwise it is equal to the argument." ie if the argument you pass to toBinaryString() is negative, it does += 2^32 before converting it to binary... which is exactly what a 2-complement is. Link to comment https://www.neowin.net/forum/topic/223959-javaconverting-integer-to-signed-binary/#findComment-584653152 Share on other sites More sharing options...
Question
NtOrZor
Is there a good way to convert a integer to a 2's complement binary and store the result as a String? In the Integer class there is a static method toBinaryString(int i) but it only handles "unsigned" integers. How do I do it with signed integers?
Link to comment
https://www.neowin.net/forum/topic/223959-javaconverting-integer-to-signed-binary/Share on other sites
9 answers to this question
Recommended Posts