There are exactly 10 possible input number strings consist of exactly 1 character:
Input typed ASCII code (in decimal)
------------- -------------------------
"0" ----> 48
"1" ----> 49
"2" ----> 50
"3" ----> 51
"4" ----> 52
"5" ----> 53
"6" ----> 54
"7" ----> 55
"8" ----> 56
"9" ----> 57
|
See it for yourself:
|
Note: 00110000(2) = 48(10)
We must construct this mapping function:
Input string ASCII code (decimal) output value (decimal)
------------- -------------------- --------------
"0" 48 ----> 0
"1" 49 ----> 1
"2" 50 ----> 2
"3" 51 ----> 3
"4" 52 ----> 4
"5" 53 ----> 5
"6" 54 ----> 6
"7" 55 ----> 7
"8" 56 ----> 8
"9" 57 ----> 9
|
Remember that: we write expressions in decimal, the compiler will translate the numbers into binary !!
The general mapping algorithm: (will work for any code assignment)
if ( s.charAt(0) == '0' )
x = 0;
else if ( s.charAt(0) == '1' )
x = 1;
else if ( s.charAt(0) == '2' )
x = 2;
else if ( s.charAt(0) == '3' )
x = 3;
else if ( s.charAt(0) == '4' )
x = 4;
else if ( s.charAt(0) == '5' )
x = 5;
else if ( s.charAt(0) == '6' )
x = 6;
else if ( s.charAt(0) == '7' )
x = 7;
else if ( s.charAt(0) == '8' )
x = 8;
else if ( s.charAt(0) == '9' )
` x = 9;
else
System.out.println("NOT a digit !\n");
|
DEMO: /home/cs255001/demo/atoi/ConvStr1DigitGen.java
Note: the computer program is actually testing (binary) numbers:
if ( s.charAt(0) == 48 )
x = 0;
else if ( s.charAt(0) == 49 )
x = 1;
else if ( s.charAt(0) == 50 )
x = 2;
else if ( s.charAt(0) == 51 )
x = 3;
else if ( s.charAt(0) == 52 )
x = 4;
else if ( s.charAt(0) == 53 )
x = 5;
else if ( s.charAt(0) == 54 )
x = 6;
else if ( s.charAt(0) == 55 )
x = 7;
else if ( s.charAt(0) == 56 )
x = 8;
else if ( s.charAt(0) == 57 )
x = 9;
else
System.out.println("NOT a digit !\n");
|
DEMO: /home/cs255001/demo/atoi/ConvStr1DigitGen2.java
An important property of the ASCII code:
|
ASCII codes of the digits:
character ASCII code (in decimal) --------- ----------- '0' 48 '1' 49 '2' 50 '3' 51 '4' 52 '5' 53 '6' 54 '7' 55 '8' 56 '9' 57 |
Because the code values increases by 1, we can use a more simple mapping algorithm
The easier mapping function is digitCode − 48 and it take advantage of the consecutive values for digits:
Input string ASCII code (decimal) output value
------------- -------------------- --------------
"0" 48 ----> 0 (= 48 - 48)
"1" 49 ----> 1 (= 49 - 48)
"2" 50 ----> 2 (= 50 - 48)
"3" 51 ----> 3 (= 51 - 48)
"4" 52 ----> 4 (= 52 - 48)
"5" 53 ----> 5 (= 53 - 48)
"6" 54 ----> 6 (= 54 - 48)
"7" 55 ----> 7 (= 55 - 48)
"8" 56 ----> 8 (= 56 - 48)
"9" 57 ----> 9 (= 57 - 48)
|
Important note: the mapping digitCode − 48 will only work when all digits have consecutive values !
Implementation of parseInt( ) for single digit strings:
s = in.next( ); // Read a String from keyboard (ASCII code !) int x = s.charAt(0) - '0'; // '0' ≡ 48 ! |
DEMO: /home/cs255001/demo/atoi/ConvStr1Digit.java
Quiz:
|
How to obtain the 2s complement representation for negative numbers:
|
Example:
If x contains 00000001 (= 1) then -x will return 11111111 |
Algorithm to convert a one-digit negative number string (e.g.: "-4"):
// Make sure the string is "-N" if ( s.length() != 2 || s.charAt(0) != '-' ) { System.out.println("Error: not 1 digit neg number !"); System.exit(1); } x = s.charAt(1) - '0'; // '0' = 48, so we subtract 48 x = -x; // Get the negative value // -x tells the CPU to perform // the 2s compl neg operation on x |
DEMO: /home/cs255001/demo/atoi/ConvStr1DigitNeg.java