How to print an integer value between [0 .. 9]

Problem description:

  • We want to print a (binary) number that is between 0 and 9 (i.e.: a single digit decimal number).

    I.e.: we only want to print one of the following binary numbers:

       00000000  00000001  00000010  00000011  00000100  
       00000101  00000110  00000111  00001000  00001001 
      

    (Note: I use 8 bits, the program will use 32 bits)

    These 2's complement codes represents the values zero, one, two, ..., nine

How to print an integer value between [0 .. 9]
 

We must perform the following 10 conversions:

        Input                             Output
 2's complement (int) code    ASCII character code used to print
 -------------------------    --------------------------------
       00000000  (zero)  -->  00110000(2) = 48(10) (char '0')
       00000001  (one)   -->  00110001(2) = 49(10) (char '1')
       00000010  (two)   -->  00110010(2) = 50(10) (char '2')
       00000011  (three) -->  00110011(2) = 51(10) (char '3')
       00000100  (four)  -->  00110100(2) = 52(10) (char '4')
       00000101  (five)  -->  00110101(2) = 53(10) (char '5')
       00000110  (six)   -->  00110110(2) = 54(10) (char '6')
       00000111  (seven) -->  00110111(2) = 55(10) (char '7')
       00001000  (eight) -->  00111000(2) = 56(10) (char '8')
       00001001  (nine)  -->  00111001(2) = 57(10) (char '9')
   

because a terminal can only show characters encoded in ASCII codes !!

The mapping algorithm for single digit numbers to their ASCII number strings

The general mapping algorithm: (will work for any code assignment)

        if ( number == 0 )
            output = "0";
         else if ( number == 1 )      // Test for 00000001
            output = "1";
         else if ( number == 2 )      // Test for 00000010
            output = "2";
         else if ( number == 3 )      // Test for 00000011
            output = "3";
         else if ( number == 4 )      // Test for 00000100
            output = "4";
         else if ( number == 5 )      // Test for 00000101
            output = "5";
         else if ( number == 6 )      // Test for 00000110
            output = "6";
         else if ( number == 7 )      // Test for 00000111
            output = "7";
         else if ( number == 8 )      // Test for 00001000
            output = "8";
         else if ( number == 9 )      // Test for 00001001
            output = "9";
         else
            System.out.println("NOT a one digit number!\n");
   

The general mapping algorithm does not depend on the code assignment scheme

An important property of the ASCII code assignment

An important property of the ASCII code:

  • The ASCII codes (= binary numbers for the digits are consecutive values

The ASCII codes for the digits are:

  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

We can take advantage of the consecutive values to write a simpler/efficient conversion/mapping algorithm

The mapping algorithm for single digit numbers to their ASCII number strings
 

A mapping function that take advantage of the consecutive code assignments of the digits:

  Input number  ASCII code (decimal)    
 -------------  --------------------  
   00000000   ---->  00110000   (48)   '0' 
   00000001   ---->  00110001   (49)   '1' 
   00000010   ---->  00110010   (50)   '2' 
   00000011   ---->  00110011   (51)   '3' 
   00000100   ---->  00110100   (52)   '4' 
   00000101   ---->  00110101   (53)   '5' 
   00000110   ---->  00110110   (54)   '6' 
   00000111   ---->  00110111   (55)   '7' 
   00001000   ---->  00111000   (56)   '8' 
   00001001   ---->  00111001   (57)   '9' 
   

Mapping function:   ASCII code = Input number + '0' (= 00110000(2) = 48(10))

Note:   This simpler mapping will only work when with ASCII codes for the digits 0, 1, 2, ..., 9

Implementing the mapping function for single digit numbers
 

Implementation of toString( ) for single digit numbers:

 int x;
 char[] c = new char[1];    
                               
 x = 4;  // *** Change to any value between 0 and 9

 c[0] = (char) (x + '0'); // ASCII code for the digit !

 /* ---------------------------------------------
    Make a string with the character(s) in c[ ]
   ---------------------------------------------- */
 String s = new String(c);
   

 

Note:   x ≥ 0 is required !!!   (What happens if x < 0 ?)

DEMO: /home/cs255001/demo/atoi/ConvInt1Digit.java

Converting negative values
 

Enhanced algorithm to handle possible negative value x:

   if ( x < 0 )
   {
      sign = -1;
      x = -x;    // Now x ≥ 0 !
   }
   else
   {
      sign = 0;
   }

   c[0] = (char) (x + '0'); // ASCII code for the digit !

   String s = new String(c);

   if ( sign == -1 )
      s = "-" + s;   // Prepend "-" symbol to number !
   

DEMO:   /home/cs255001/demo/atoi/ConvInt1DigitNeg.java