jQuery in Action, 3rd edition
The moose likes Beginning Java and the fly likes Converting an int to a char Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Converting an int to a char" Watch "Converting an int to a char" New topic

Converting an int to a char

Kelly Harris

Joined: Mar 27, 2002
Posts: 22
There does not seem to be an easy way to take an int (like a 32) and return its character representative ( a Space). I looked through the Integer and Character classes along with some other help topics. This seems so simple but I cannot find the answer.
Kelly Harris
Jason Kretzer
Ranch Hand

Joined: May 31, 2001
Posts: 280
I have no idea if this will work but...
Integer i = new Integer(32);
char c = (char)i;
This code is only proved correct, I have not tested it.


Jason R. Kretzer<br />Software Engineer<br />System Administrator<br /><a href="http://alia.iwarp.com" target="_blank" rel="nofollow">http://alia.iwarp.com</a>
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451
Hi Kelly,
In what context are you speaking? There are various I/O methods that use ints instead of chars. Why can't you just typecast the int to a char? :

Hope this helps
Michael Morris

Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius - and a lot of courage - to move in the opposite direction. - Ernst F. Schumacher
Kelly Harris

Joined: Mar 27, 2002
Posts: 22
Thanks, I feel real stupid now. I was trying to type cast it into a String rather than just a char. I was thinking too hard (smell the smoke).
Karl Laird
Ranch Hand

Joined: Jul 14, 2001
Posts: 67
Just reiterating that the method mentioned in the second post will not work as you are trying to cast an object (Integer) into a simple type (char). This should throw a compile time exception. The method mentioned in the third post is correct - directtly cast from simple type (int) to simple type (char).

Of course if you really want it to be a string you could always use
String myString = myInt + "";

The Eagle sneers at the Peacock<p>Systems Administrator<br />OrderWare Solutions Ltd<br /><a href="http://www.orderware.net" target="_blank" rel="nofollow">http://www.orderware.net</a>
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
One thing to note about casting chars and other primitives. The following code is legal because a char (2 bytes) is smaller than an int (4 bytes), therefore a windening conversion is taking place and an implicit cast is done.

However, the following code is illegal, even though both data types are 2 bytes.

The reason this is illegal is because the data type char, unlike other primitive types (boolean and void excluded), is unsigned. Therefore, the range of a char does not exactly match the range of a short (and vice versa).
Just something to keep in mind.

SCJP Tipline, etc.
I agree. Here's the link: http://aspose.com/file-tools
subject: Converting an int to a char
It's not a secret anymore!