import java.io.*; class AtoiDemo { /* ----- PutBit(b): prints "1" if b != 0 and "0" otherwise ----- */ private static void PutBit(int b) { if (b != 0) System.out.print("1"); else System.out.print("0"); } /* ----- PrintAsciiCode(c): prints an 8 bit quantity in legible "0" and "1" ----- */ private static void PrintAsciiCode(char c) { int i; for (i = 7; i >= 0; i--) PutBit(c & (1 << i)); } /* ----- PrintIntCode(c): prints a 32 bit quantity in legible "0" and "1" ----- */ private static void PrintIntCode(int k) { int i; for (i = 31; i >= 0; i--) PutBit(k & (1 << i)); } /* --- atoi(s): Ascii to Integer conversion function --- */ private static int atoi(String s) { int startpos, sign; int value; int i; System.out.print(" Input string: "); for (i = 0; i < s.length(); i++) System.out.print(" " + s.charAt(i)); System.out.println(); System.out.println(); /* ------- Convert string to two's compl. representation ------- */ if (s.charAt(0) == '-') { sign = -1; startpos = 1; } else { sign = 1; startpos = 0; } System.out.println(" sign = " + sign); value = 0; for (i = startpos; i < s.length(); i++) { System.out.print(" value is now = " + value); System.out.print(" ("); PrintIntCode(value); System.out.print(")"); System.out.println("\tnext digit is " + s.charAt(i)); value = ((int) s.charAt(i) - (int) '0') + 10*value; } System.out.print(" value is now = " + value); System.out.print(" ("); PrintIntCode(value); System.out.println(") - no more digits !"); System.out.println(); System.out.println(" Incorporating sign..."); System.out.print(" Before: "); PrintIntCode(value); System.out.println(); if (sign == -1) value = -value; System.out.print(" After: "); PrintIntCode(value); System.out.println(); System.out.print(" Final value is = " + value); System.out.println(); return(value); } public static void main(String[] args) throws IOException { BufferedReader stdin = new BufferedReader (new InputStreamReader(System.in)); String s; int value; int i; System.out.println("Program reads a char string and converts "+ "it to int representation"); System.out.println(); while (true) { System.out.println(); System.out.print("Enter a number: "); // Ordinarily, one would use: // i = Integer.parseInt (stdin.readLine()); // Java will do the conversion for us... // We are here to reveil to you what is // going on inside Integer.parseInt() s = stdin.readLine(); // ****************************** // Show the characters entered // ****************************** System.out.println("What you have actually entered are:\n"); System.out.print("\t"); for (i = 0; i < s.length(); i++) { PrintAsciiCode(s.charAt(i)); System.out.print(" (" + (int) s.charAt(i)); System.out.print(") "); } System.out.println("\n"); // ********************************************** // Show ASCII codes for the characters entered // ********************************************** System.out.print("These are the ASCII codes for char: "); for (i = 0; i < s.length(); i++) { System.out.print(" " + s.charAt(i)); } System.out.print("\n\n"); // ********************************************** // Now convert ! // ********************************************** System.out.print("Now converting ASCII codes to "+ "2's complement representation..\n"); value = atoi(s); System.out.print("\nTwo's complement representation is: "); PrintIntCode(value); System.out.print("\n"); System.out.println("Decimal value = " + value); } } }