Hello,
Here's a real rube question... I happen to want the ASCII value of a
character variable. Seriously. I've always known that Java uses
Unicode, but I also know that the Unicode values for the basic Latin
character set are the same as ASCII. But when I try to get a numeric
representation of a character, I get things like 'A' = 10 and 'a' = 10.
What I want is 'A' = 65, or 41, or something of that ilk. Is this
possible?
Thanks,
DaveO.
Gordon Beaton - 13 Feb 2006 18:01 GMT
> What I want is 'A' = 65, or 41, or something of that ilk. Is this
> possible?
Yes. All you need to do is realize that char is *already* an integer,
so no conversion is necessary to get the value in most situations.
However System.out.println('A') doesn't work as expected because
println() is overloaded and will do what it *thinks* you want instead.
Still, all you need is a cast (no conversion):
System.out.println((int)'A');
/gordon

Signature
[ do not email me copies of your followups ]
g o r d o n + n e w s @ b a l d e r 1 3 . s e
DaveO. - 13 Feb 2006 18:34 GMT
So, change "rube" to "ruby slippers..."
Roedy Green - 13 Feb 2006 21:59 GMT
>Here's a real rube question... I happen to want the ASCII value of a
>character variable. Seriously. I've always known that Java uses
[quoted text clipped - 3 lines]
>What I want is 'A' = 65, or 41, or something of that ilk. Is this
>possible?
there is nothing to do. A char IS the number.
char c = 'A';
int cp = c+1;
System.out.println( cp );
will print out 66;

Signature
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.