Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

converting from integer to char

 
Emnaki Chih
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Emnaki Chih
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alright i see, but is there a way to reverse the conversion that getNumericValue() does?
 
Paul Clapham
Sheriff
Posts: 21416
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1078
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
lol
sorry meant to put ""
yes he prob does lol
 
Ken Blair
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That wouldn't compile either. "" + int will return a String which cannot be converted to char.
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic