It's not a secret anymore!*
The moose likes Beginning Java and the fly likes converting from integer to char Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "converting from integer to char" Watch "converting from integer to char" New topic
Author

converting from integer to char

Emnaki Chih
Ranch Hand

Joined: Sep 01, 2004
Posts: 30
When I execute this:

I get this result:

Where the strange symbol at the end is indeed a strange symbol.
So my question is how do I convert back to a char what I've converted with method getNumericValue()?
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
If you scare up an ASCII Chart you can see that 11 or 0x0B is a vertical tab character. On some devices I'd guess this tabs down or advances paper in a printer or something. Since it's in a range that is not printable, you get the little square symbol. I think you'd be safe saying 32-126 are printable. I used to use the "extended" set 127 and up on DOS.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Emnaki Chih
Ranch Hand

Joined: Sep 01, 2004
Posts: 30
Alright i see, but is there a way to reverse the conversion that getNumericValue() does?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Well, you can't. Both 'B' and 'b' are converted to 11 by the getNumericValue() method, just for one example. There are about 10 different Unicode characters that are converted to 1 -- you can find out what they are by writing a simple test program.

In my experience answering these questions, usually people who are using getNumericValue() don't understand what it does, and it usually isn't the right solution to their problem. What was your original question, anyway? I mean the one that you thought getNumericValue() was the answer to?
stephen shields
Greenhorn

Joined: Jun 05, 2006
Posts: 20
when i was in school my professor told me a very useful thing

to convert a number to a char or to a string then u do the following

char mychar = ''+ somenumber;
String mystring = ""+ somenumber;
Ken Blair
Ranch Hand

Joined: Jul 15, 2003
Posts: 1078
Originally posted by stephen shields:
when i was in school my professor told me a very useful thing

to convert a number to a char or to a string then u do the following

char mychar = ''+ somenumber;
String mystring = ""+ somenumber;


The first line isn't valid and won't compile. The second line would have been better served by a simple call to Integer.toString(somenumber) and would avoid creating a StringBuffer/Builder unnecessarily. Your professor needs to take a class.
stephen shields
Greenhorn

Joined: Jun 05, 2006
Posts: 20
lol
sorry meant to put ""
yes he prob does lol
Ken Blair
Ranch Hand

Joined: Jul 15, 2003
Posts: 1078
That wouldn't compile either. "" + int will return a String which cannot be converted to char.
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
So back to the original question. Make sure you understanad what Character.getNumericalValue() actually does. I agree with Paul that this method is probably not the solution to your original task. So let's back up a little bit. Can you describe what this code is supposed to do? That is what is the original problem or the context where you decided you might need to use this method? If there is a better solution, I'm sure we can help you find it from there.

Layne


Java API Documentation
The Java Tutorial
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: converting from integer to char
 
Similar Threads
Character.getNumericValue()
convert character to integer
char primitive default value
Unicode Migraine!
manpulating Unicode values..