how can i convert int to char or string

now les say: i want to convert int to char
ex:
int a = 3
char b = '3'
note they both are same number

or int to String
int a = 35;
String c = "35";

note i already tried

b = (char)a; but failed. it print weird symbol

Dani AI

Generated

Casting an int to char is interpreting the number as a Unicode code point, not as the text for that digit. For example, numeric 3 cast to char becomes U+0003 (a non‑printable control), while the character '3' is U+0033 (decimal 51). That is why your direct cast produced a “weird” symbol.

If the goal is the textual digit for a single decimal digit (0–9), map the numeric value into the digit characters rather than casting the raw number. The usual approaches for this were already suggested by , and for strings, and by and for single-digit chars. Use a digit-to-character mapping (or Character.forDigit for base-aware mapping) for single digits, and use string conversion when the number may have multiple digits or a sign.

A few practical notes not shown above:

  • Use string conversion for any multi-digit or negative value — a char can only hold one UTF-16 code unit.
  • Prefer String.valueOf (or the primitive variant) when you might have an Integer object that could be null — calling toString() on a null Integer throws NPE, while String.valueOf returns the string "null".
  • For formatting (leading zeros, locale, etc.) use formatting APIs rather than manual concatenation.
  • For Unicode care: char is a 16‑bit code unit; code points above U+FFFF need surrogate pairs.

If you need to get a numeric value back from a digit character, a simple subtraction works:

char ch = '3';
int digit = ch - '0';  // digit == 3

For Unicode or non-ASCII digits, use Character.getNumericValue(ch) instead.

Recommended Answers

All 6 Replies

Try Integer.toString(a) or String.valueOf(a)

you can try this

from int to string

int a=3;
String s=a+"":

from int to char

char c = (char)(((int)'0')+a);

however here the value of 'a' should be a single digit number

commented: good solution +4

The right way to convert an int to a char is using the Character.forDigit method.

char b = Character.forDigit(a,10);
commented: really new thing i get to learn... +4

you can convert it in different way like:

1) built-in convert

int a1 = 5;
String s = ""+a1;

2) integer convert

int a2 = 5;
String s2 = Integer.toString(a2);

3) format convert

int a3 = 9;
String s3 =String.format("%d", a3);

Also one possible solution is:

StringBuilder sb = new StringBuilder();
sb.append("");
sb.append(i);
String strI = sb.toString();

or simply

Integer i;
String s = i.toString();

or

int aInt = 1;
String aString = Integer.toString(aInt);
commented: Best and most complete answer +4

and for a String, you could use
String newString = String.valueOf(a);

One more thing is:
In-fact String.valueOf() method is overloaded to accept almost all primitive type so you can use it convert char, double, float or any other data type into String. Static binding is used to call corresponding method in Java. Here is an example of int to String using String.valueOf().

String price = String.valueOf(123);

instead of 123 you can also pass int variable

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.