import java.util.Scanner; class Atoi { /* --- Return 10^k in Binary 2's complement code --- */ public static int Power(int k) { int r = 1; // Stores: 00000000000000000000000000000001 in variable r for (int i = 0; i < k; i++ ) { r = r * 10; // The Java compiler will convert 10 into the // binary number 00000000000000000000000000001010 // This statement will multiply bin number r // with 00000000000000000000000000001010 } return r; // Returns a BINARY number in 2's compl // that represents the VALUE 10^k } /* --- parseInt(s): Ascii to Integer conversion function --- */ public static int myParseInt(String s) { int[] digit = new int[20]; // Used to store individual digits in String s int value, sign; int i, pos; int len; /* ------------------------- Check for negative sign ------------------------- */ if (s.charAt(0) == '-') { sign = -1; pos = 1; // Start processing at charAt(1) } else { sign = 0; pos = 0; // Start processing at charAt(0) } /* ------------------------------------------- Convert each character digit into 2s compl (and keep a count on the number of digits ------------------------------------------- */ len = 0; // len counts # digits (without the leading '-') for ( i = pos; i < s.length(); i++ ) // Convert ASCII digits { digit[len] = s.charAt(i) - 48; len++; // Count # digits in number } /* ----------------------------------------------------------- ## DEBUG: Print the digits and its position in the number ----------------------------------------------------------- */ System.out.println(); System.out.println(" Digit Position"); System.out.println("============================="); for (int k = 0; k < len; k++) { pos = (len - 1) - k; System.out.println("\t`" + digit[k] + "' pos = " + pos); } System.out.println(); /* ------------------------------------------ Compute the absolute value of the number ------------------------------------------ */ value = 0; for (int k = 0; k < len; k++) { pos = (len - 1) - k; value = value + digit[k]*Power( pos ); // High school knowledge.... } /* ======================================================== Negate 2's complement representation if first character of input was '-' ========================================================= */ if (sign == -1) value = -value; // Compute the negative value (flip bits and add 1 !!) return(value); // Return a BINARY 2's compl code } public static void main(String[] args) { Scanner in = new Scanner(System.in); String s1, s2; int i1, i2; System.out.print("Enter an integer (string) s1: "); s1 = in.next(); System.out.println("We read the numeric string: \"" + s1 + "\""); i1 = myParseInt(s1); // Same effect as: i1 = Integer.parseInt(s1); !!!! System.out.println("The integer VALUE for this numeric string is: " + i1); System.out.print("\nEnter another integer (string) s2: "); s2 = in.next(); System.out.println("We read the numeric string: \"" + s2 + "\""); i2 = myParseInt(s2); // Same effect as: i1 = Integer.parseInt(s1); !!!! System.out.println("The integer VALUE for this numeric string is: " + i2); System.out.println("==================================================="); System.out.println("The sum of the integer values = " + (i1+i2) ); } }