Convert int to char in java

Below is a snippet of code,

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

But I get an empty output.

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

I will get 1 as output.

Can someone explain this? And if I want to convert int to char, as in the first fragment, what should I do?

+102
java char int
Aug 01 '13 at 3:51
source share
14 answers
 int a = 1; char b = (char) a; System.out.println(b); 

prints char using the value ascii 1 (the beginning of the char header, which cannot be printed).

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

will print a char with an ascii value of 49 (one matches "1")

If you want to convert a digit (0-9), you can add 48 to it and say it, or something like Character.forDigit(a, 10); .

If you want to convert int , as to ascii value, you can use Character.toChars(48) , for example.

+91
Aug 01 '13 at 3:52 on
source share

My answer is similar to jh314's answer, but I will explain a little deeper.

What you should do in this case:

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

Here we used '0' because the characters are actually represented by ASCII values. "0" is the character represented by 48.

We typed (a + '0') and, to add them, Java converted '0' to its ASCII value, which is 48, and a is 1, so the sum is 49. Then we did the following:

 (char)(49) 

We threw int on char . ASCII, equivalent to 49, is "1". You can convert any digit to a character this way, and it will be smarter and better than using the .toString() method and then subtracting the digit with the .charAt() method.

+47
Jun 04 '15 at 21:05
source share

It seems that 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 char back to int:

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

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

+36
09 Oct '15 at 6:51
source share
 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); 

In doing so, you will get the decimal value as a char type. I used charAt () with index 0, because the only value in this line is "a", and, as you know, the position of "a" in this line starts with 0.

Sorry if my English is not well explained, I hope this helps you.

+9
Nov 21 '14 at 3:46
source share

No one answered the real “question” here: you convert int to char correctly; in the ASCII table, decimal 01 is the "beginning of the header", a non-printing character. Try to find the ASCII table and convert the int value between 33 and 7E; which will give you characters to watch.

+1
Sep 12 '16 at 17:28
source share

Whenever you enter an integer in integers in char, it returns the ascii value of this int (go through the ascii table once for a better understanding)

  int a=68; char b=(char)a; System.out.println(b);//it will return ascii value of 68 //output- D 
+1
Dec 29 '16 at 5:02
source share

If we are talking about class types, not primitives, the following trick needs to be done:

 Integer someInt; Character someChar; someChar = (char)Integer.parseInt(String.valueOf(someInt); 
+1
Dec 16 '17 at 10:06 on
source share
 int a = 1; char b = (char) (a + 48); 

In ASCII, each character has its own number. And char '0' is 48 for the decimal number, '1' is 49 and so on. So if

 char b = '2'; int a = b = 50; 
+1
Nov 02 '18 at 16:59
source share

There is an int in java a char. Your first fragment prints a character matching the value 1 in the default encoding scheme (which is probably Unicode). The Unicode character U + 0001 is a non-printable character, so you do not see any output.

If you want to print the character "1", you can look at the value "1" in the encoding scheme used. In Unicode, this is 49 (same as ASCII). But this will only work for numbers 0-9.

You might be better off using a string rather than a char, and use the built-in Java toString() method:

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

This will work regardless of your system encoding and will work for multi-digit numbers.

0
Aug 01 '13 at 4:42
source share

if you want to print ascii characters based on their ascii code and don't want to go beyond that (e.g. Unicode characters), you can define your variable as a byte, and then use a converter (char). i.e:.

 public static void main(String[] args) { byte b = 65; for (byte i=b; i<=b+25; i++) { System.out.print((char)i + ", "); } 

BTW, the ascii code for the letter "A" is 65

0
Jun 29 '17 at 14:15
source share

Make sure the integer value is an ASCII alphabet / character value.

If not, do it.

 for eg if int i=1 

then add 64 to it so that it becomes 65 = the ASCII value of 'A'. Then use

 char x = (char)i; print x // 'A' will be printed 
0
Feb 20 '18 at 19:12
source share

look at the following program for a complete conversion concept

 class typetest{ public static void main(String args[]){ byte a=1,b=2; char c=1,d='b'; short e=3,f=4; int g=5,h=6; float i; double k=10.34,l=12.45; System.out.println("value of char variable c="+c); // if we assign an integer value in char cariable it possible as above // but it not possible to assign int value from an int variable in char variable // (d=g assignment gives error as incompatible type conversion) g=b; System.out.println("char to int conversion is possible"); k=g; System.out.println("int to double conversion is possible"); i=h; System.out.println("int to float is possible and value of i = "+i); l=i; System.out.println("float to double is possible"); } } 

hope this helps at least something

0
Jul 04 '19 at 15:30
source share
 public class String_Store_In_Array { public static void main(String[] args) { System.out.println(" Q.37 Can you store string in array of integers. Try it."); String str="I am Akash"; int arr[]=new int[str.length()]; char chArr[]=str.toCharArray(); char ch; for(int i=0;i<str.length();i++) { arr[i]=chArr[i]; } System.out.println("\nI have stored it in array by using ASCII value"); for(int i=0;i<arr.length;i++) { System.out.print(" "+arr[i]); } System.out.println("\nI have stored it in array by using ASCII value to original content"); for(int i=0;i<arr.length;i++) { ch=(char)arr[i]; System.out.print(" "+ch); } } } 
-one
03 Feb '18 at 15:32
source share

If you want to convert a character to a corresponding integer, you can do something like this:

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

This is because there are some elements in ASCII that cannot be printed normally.

For example, numbers 97 through 122 are integers corresponding to lowercase letters a to z.

-2
Oct 26 '16 at 22:57
source share



All Articles