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
Convert int to char in java - Stack Overflow
Converting characters to integers in Java - Stack Overflow
Example to Convert int to char in Java
Videos
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));
}int a = 1;
char b = (char) a;
System.out.println(b);
will print out the char with Unicode code point 1 (start-of-heading char, which isn't printable; see this table: C0 Controls and Basic Latin, same as ASCII)
int a = '1';
char b = (char) a;
System.out.println(b);
will print out the char with Unicode code point 49 (one corresponding to '1')
If you want to convert a digit (0-9), you can add 48 to it and cast, or something like Character.forDigit(a, 10);.
If you want to convert an int seen as a Unicode code point, you can use Character.toChars(48) for example.
My answer is similar to jh314's answer but I'll explain a little deeper.
What you should do in this case is:
int a = 1;
char b = (char)(a + '0');
System.out.println(b);
Here, we used '0' because chars are actually represented by ASCII values. '0' is a char and represented by the value of 48.
We typed (a + '0') and in order to add these up, Java converted '0' to its ASCII value which is 48 and a is 1 so the sum is 49. Then what we did is:
(char)(49)
We casted int to char. ASCII equivalent of 49 is '1'. You can convert any digit to char this way and is smarter and better way than using .toString() method and then subtracting the digit by .charAt() method.
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();