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.
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.
converting int -> char
converting ch to int java
How can I convert a char to int in Java? - Stack Overflow
Need help with java converting int to unicode
If the int represents a UTF-16 code unit, just cast it to char.
char c = (char) myInt;
If the int represents a Unicode code point, use Character.toChars()
char[] chars = Character.toChars(myInt);
Note that a Character or char in Java does not represent a Unicode code point or character, rather it represents a UTF-16 code unit.
More on reddit.comCan we convert a char array to String in Java?
Can you assign a char to an int in Java?
How to take char as input after an integer in Java?
Videos
Hey, im new to programming and i have a problem with converting an integer into a character.
This is a code example:
char ch = '0';
int i = 1;
ch = char(i);
println(ch);
I dont want to convert to unicode. The outcome should just be that '1' is being stored as a char.
Is there an easy way to convert like that?
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));
}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
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