Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / February 2006

Tip: Looking for answers? Try searching our database.

ASCII value of char

Thread view: 
DaveO. - 13 Feb 2006 17:35 GMT
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.



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.