The ASCII table is arranged so that the value of the character '9' is nine greater than the value of '0'; the value of the character '8' is eight greater than the value of '0'; and so on.
So you can get the int value of a decimal digit char by subtracting '0'.
char x = '9';
int y = x - '0'; // gives the int value 9
Answer from khelwood on Stack OverflowThe ASCII table is arranged so that the value of the character '9' is nine greater than the value of '0'; the value of the character '8' is eight greater than the value of '0'; and so on.
So you can get the int value of a decimal digit char by subtracting '0'.
char x = '9';
int y = x - '0'; // gives the int value 9
I you have the char '9', it will store its ASCII code, so to get the int value, you have 2 ways
char x = '9';
int y = Character.getNumericValue(x); //use a existing function
System.out.println(y + " " + (y + 1)); // 9 10
or
char x = '9';
int y = x - '0'; // substract '0' code to get the difference
System.out.println(y + " " + (y + 1)); // 9 10
And it fact, this works also :
char x = 9;
System.out.println(">" + x + "<"); //> < prints a horizontal tab
int y = (int) x;
System.out.println(y + " " + (y + 1)); //9 10
You store the 9 code, which corresponds to a horizontal tab (you can see when print as String, bu you can also use it as int as you see above
converting ch to int java
Converting characters to integers in Java - Stack Overflow
Convert int to char in place.
Example to Convert int to char in Java
i have an assignment where we have to import a file of letters and convert these to ascii numbers where each even number is a colour and each odd is the background. I am struggling on how to convert ch to an int.
heres my code so far
import sheffield.*;
import java.util.Arrays;
public class Assignment2Practice {
public static void main(String args[]) {
EasyReader fileInput= new EasyReader("duck.txt"); // read in duck file
String duckText = fileInput.readString(); // convert to string variable
char [][] duckArray = new char [130][130];
for (int j =0; (j <130); j++) {
for (int i=0; (i < 130); i++){
char ch = duckText.charAt(i); // j[1], v[2]
int int1= ch;
int int2 = (int) ch;
duckArray[j][i] = ch;
}
}
System.out.println(Arrays.deepToString(duckArray));
}Character.getNumericValue(c)
The java.lang.Character.getNumericValue(char ch) returns the int value that the specified Unicode character represents. For example, the character '\u216C' (the roman numeral fifty) will return an int with a value of 50.
The letters A-Z in their uppercase ('\u0041' through '\u005A'), lowercase ('\u0061' through '\u007A'), and full width variant ('\uFF21' through '\uFF3A' and '\uFF41' through '\uFF5A') forms have numeric values from 10 through 35. This is independent of the Unicode specification, which does not assign numeric values to these char values.
This method returns the numeric value of the character, as a nonnegative int value;
-2 if the character has a numeric value that is not a nonnegative integer;
-1 if the character has no numeric value.
And here is the link.
As the documentation clearly states, Character.getNumericValue() returns the character's value as a digit.
It returns -1 if the character is not a digit.
If you want to get the numeric Unicode code point of a boxed Character object, you'll need to unbox it first:
int value = (int)c.charValue();