There is a 1-to-1 correspondence between String representation and 2's complement representation of numbers:
String representation 2's complement representation (32 bits)
----------------------- ----------------------------------
"0" 00000000000000000000000000000000
"1" 00000000000000000000000000000001
"2" 00000000000000000000000000000010
"3" 00000000000000000000000000000011
... ...
"-1" 11111111111111111111111111111111
"-2" 11111111111111111111111111111110
"-3" 11111111111111111111111111111101
... ...
|
/* ---------------------------------------------------------------
Trivial conversion: match the input string to every possible
number string and return the corresponding
2's complement binary number
--------------------------------------------------------------- */
public static int parseInt( String s )
{
if ( s.equals("0") )
return 0; // The return statement will return the binary number
// 00000000000000000000000000000000 !!
else if ( s.equals("1") )
return 1; // returns: 00000000000000000000000000000001
else if ( s.equals("2") )
return 2; // returns: 00000000000000000000000000000010
...
else if ( s.equals("10") )
return 10; // returns: 00000000000000000000000000001010
else if ( s.equals("11") )
return 11; // returns: 00000000000000000000000000001011
...
else if ( s.equals("-1") )
return -1; // returns: 11111111111111111111111111111111
else if ( s.equals("-2") )
1eturn -2; // returns: 11111111111111111111111111111110
...
}
|
|