Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert int to char in java

Tags:

java

char

int

People also ask

Can we convert int to char in java?

We can also convert integer into character in Java by adding the character '0' to the integer data type. This is similar to typecasting. The syntax for this method is: char c = (char)(num + '0');

How do I convert an int to a char?

char a = Character. forDigit(num1, 10); We have used the forDigit() method converts the specified int value into char value. Here, 10 and 16 are radix values for decimal and hexadecimal numbers respectively.

Can I convert int to String?

We can convert int to String in java using String. valueOf() and Integer. toString() methods. Alternatively, we can use String.

How do I convert a number to a char array?

One method to convert an int to a char array is to use sprintf() or snprintf(). This function can be used to combine a number of variables into one, using the same formatting controls as fprintf(). int sprintf ( char *buf, const char *format, ... ); int snprintf( char *buf, size_t n, const char *format, ... );


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.


It seems like you are looking for the Character.forDigit method:

final int RADIX = 10;
int i = 4;
char ch = Character.forDigit(i, RADIX);
System.out.println(ch); // Prints '4'

There is also a method that can convert from a char back to an int:

int i2 = Character.digit(ch, RADIX);
System.out.println(i2); // Prints '4'

Note that by changing the RADIX you can also support hexadecimal (radix 16) and any radix up to 36 (or Character.MAX_RADIX as it is also known as).


int a = 1;
char b = (char) a;
System.out.println(b);

hola, well i went through the same problem but what i did was the following code.

int a = 1
char b = Integer.toString(a).charAt(0);
System.out.println(b);

With this you get the decimal value as a char type. I used charAt() with index 0 because the only value into that String is 'a' and as you know, the position of 'a' into that String start at 0.

Sorry if my english isn't well explained, hope it helps you.


you may want it to be printed as '1' or as 'a'.

In case you want '1' as input then :

int a = 1;
char b = (char)(a + '0');
System.out.println(b);

In case you want 'a' as input then :

int a = 1;
char b = (char)(a-1 + 'a');
System.out.println(b);

java turns the ascii value to char :)