Input number strings that that consist of exactly 1 character
 

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
   

Input number strings that that consist of exactly 1 character
 

See it for yourself:

  • Type a number string that consists of 1 digit:
       

  • The computer program will receive the following:
       

Note: 00110000(2) = 48(10)

The mapping function for input strings consisting of exactly 1 digit
 

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 mapping function for input strings consisting of exactly 1 digit

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

The mapping function for input strings consisting of exactly 1 digit

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 assignment

An important property of the ASCII code:

  • The ASCII codes (= binary numbers) for the digits have consecutive ASCII code values

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 mapping function for input strings consisting of exactly 1 digit
 

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 !

Implementing the mapping function for input strings consisting of exactly 1 digit
 

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 to test your understanding
 

Quiz:

  • What happens when you enter:

            :

              ???

How to handle negative numbers
 

How to obtain the 2s complement representation for negative numbers:

  • The negation operator (-) computes the negative 2s complement representation

Example:

   If x contains 00000001  (= 1)
   then  -x  will return  11111111
   

How to handle negative numbers
 

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