Algorithm to convert an integer number string into 2s compl representation
 

What does the conversion algorithm do:

   Input:    An ASCII number string  
  
   Example:  "-12"
             00101101 00110001 00110010   (ASCII codes)

   Output:   The 2s complement code
             for the input number string 

   Output for the example:
	     11111111111111111111111111110100
   

Algorithm to convert an integer number string into 2s compl representation
 

The conversion algorithm in pseudo code:

                                Sample Input: s = "-12"

                                00101101 00110001 00110010 

 (1) pos = 0; // Default starting position to process input

     If (s.charAt(0) == '-')
     {
       Remember negative sign
       pos = 1;    // Start processing at char pos 1
     }
                                         00110001 00110010 
   

Algorithm to convert an integer number string into 2s compl representation
 

The conversion algorithm in pseudo code:

                                Sample Input: s = "-12"

                                00101101 00110001 00110010 
                                         00110001 00110010 
 (2) Map each ASCII code
     to it's 2s complement
     equivalent
                                         00000001 00000010 
  


   

Algorithm to convert an integer number string into 2s compl representation
 

The conversion algorithm in pseudo code:

                                Sample Input: s = "-12"

                                00101101 00110001 00110010 
                                         00110001 00110010 
                                         00000001 00000010 
 (3) Compute the value
     (in binary) using
     formula ∑dk×10k:
                                  00000001×000010101 
                                + 00000010×000010100
                                = 00001100

  Problem: how to find the exponent ?
   

(I used 8 bits 2s complement representation, the Java program will use 32 bits (int) representation)

Side note: finding the factor for a digit
 

This is how to find the multiplication factor (exponent) for a digit in the number string:

  Example:
                      charAt(k=0)
                         |
                         V
    Input number:        2   5   6   7   (length = 4 digits)

    Factor:             103 102 101 100

                      -------------------> charAt(k)
    Loop variable k =    0   1   2   3
    Exponent        =    3   2   1   0

    Exponent (= Position)  =  (length - 1) - k
  

Algorithm to convert an integer number string into 2s compl representation
 

The conversion algorithm in pseudo code:

                                Sample Input: s = "-12"

                                00101101 00110001 00110010 
                                         00110001 00110010 
                                         00000001 00000010 
                                00001100

 (4) If (number was negative)
     {
        result = -result;
     }
				11110100  (<--- -12(10))
   

(I used 8 bits 2s complement representation, the Java program will use 32 bits (int) representation)

Algorithm to convert an integer number string into 2s compl representation

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

Postscript: a more "elegant" algorithm

There is a more elegant (but harder to understand) conversion algorithm:

   public static int myParseInt(String s)
   {
      int  value, sign;
      int  pos;

      /* -------
         Check for negative sign
         ------- */
      if (s.charAt(0) == '-')
      { sign = -1;
        pos = 1;           // Start at digit s.charAt(1)
      }
      else
      { sign = 1;
        pos = 0;           // Start at digit s.charAt(0)
      }

      /* ------------------------------------------
         Compute the absolute value of the number
         ------------------------------------------ */
      value = 0;
      for (int k = pos; k < s.length(); k++)
      {
         value = 10*value + ( s.charAt(k) - 48 ); 
      }

      /* ========================================================
         Return the 2s complement representation
         ========================================================= */
      return (sign*value);  // If sign == -1, we will return 
			    // the 2s complement negative value !
   }