|
Example:
|
Here are some output when you run /home/cs255001/demo/atoi/JavaInput_3.java using 1 digit inputs:
Enter input: 0 String = 0 ASCII codes (in decimal) for the string: 48 |
Notice that:
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
|
This is because:
Try it yourself. Type any number here:
The input corresponds to these binary numbers:
Convert the binary number to decimal values and you will find the above mapping.
if ( input = input1 )
output = output1;
else if ( input = input2 )
output = output2;
else
....
|
//
// File: /home/cs255001/demo/atoi/JavaInput_5.java
//
// Read in 1 character and add 1 to the input
//
import java.util.Scanner;
public class JavaInput_4
{
public static void main(String[] args) throws Exception
{
Scanner in = new Scanner(System.in);
String s = null;
int x = 0;
while ( true )
{
System.out.print("Enter input: ");
s = in.next( ); // Read a String from key board
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("Input '"+s.charAt(0)+"' is NOT a digit !\n");
continue;
}
System.out.println("integer value x = " + x);
x = x + 1; // Show that we can compute with x !
System.out.println("x + 1 = " + x + "\n");
}
}
|
How to run the program:
|
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 also perform the mapping using this mapping function:
input - '0' (≡ input - 48)
|
Because:
Note: '0' ≡ 48 !!!
Input typed ASCII code (in decimal) mapped input value (in decimal)
------------- ------------------------- -------------------------
'0' ----> 48 48 - 48 = 0
'1' ----> 49 49 - 48 = 1
'2' ----> 50 50 - 48 = 2
'3' ----> 51 51 - 48 = 3
'4' ----> 52 52 - 48 = 4
'5' ----> 53 53 - 48 = 5
'6' ----> 54 54 - 48 = 6
'7' ----> 55 55 - 48 = 7
'8' ----> 56 56 - 48 = 8
'9' ----> 57 57 - 48 = 9
|
Here is a Java program that reads in one ASCII digit code, converts it using the above mapping function into an int (2's complement code) so that we can perform an addition (+1) to the value:
//
// File: /home/cs255001/demo/atoi/JavaInput_4.java
//
// Read in 1 character and add 1 to the input
//
import java.util.Scanner;
public class JavaInput_4
{
public static void main(String[] args) throws Exception
{
Scanner in = new Scanner(System.in);
String s = null;
while ( true )
{
System.out.print("Enter input: ");
s = in.next( ); // Read a String from key board
if ( s.length() > 1 )
{
System.out.println("Error: you must type 1 digit !");
System.out.println("Try again\n");
continue;
}
System.out.println("ASCII codes (in decimal) = " + (int) s.charAt(0) );
int x = (int) s.charAt(0);
x = x - '0'; // Convert ASCII repr to 2's complement
System.out.println("integer value x = " + x);
x = x + 1; // Now we can add !!!
System.out.println("After x+1, x = " + x);
}
}
}
|
How to run the program:
|
|
|
int x = (int) s.charAt(0);
// Convert ASCII repr to 2's complement
if ( s.charAt(0) == '0' ) // This test if ASCII code = 48
x = 0; // This assigns 0000.00 to x !!
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;
|
This is the most general way to convert an ASCII code to its corresponding 2s complement value !!