This week's book giveaway is in the Android forum.
We're giving away four copies of Head First Android and have Dawn & David Griffiths on-line!
See this thread for details.
The moose likes Java in General and the fly likes how to print ASCII value of a number ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Head First Android this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "how to print ASCII value of a number ?" Watch "how to print ASCII value of a number ?" New topic

how to print ASCII value of a number ?

Bin Maths

Joined: May 29, 2001
Posts: 5
I just want to print ASCII value of say "A" to print to console using System.out.println(). ie "61"
And visaversa.
Can anyone provide me the solution ASAP.?
Bosun Bello
Ranch Hand

Joined: Nov 06, 2000
Posts: 1510
Just cast it to an int.
char myChar = 'A';
System.out.println(myChar + "" + (int)myChar);


So much trouble in the world -- Bob Marley
Bin Maths

Joined: May 29, 2001
Posts: 5
Thanks, it was that simple?
Junilu Lacar

Joined: Feb 26, 2001
Posts: 6264

Quickest way is to cast it:
System.out.println((int)'A'); // 65
System.out.println((char)65); // A

Friendly reminder: asking for solutions "ASAP" doesn't improve your chances of getting an answer. Some folks may find that kind of request presumptuous/borderline rude and would ignore your message just for spite even if they knew the answer.

Junilu - [How to Ask Questions] [How to Answer Questions]
Mirai Chelean

Joined: Jun 27, 2005
Posts: 23
You can try to play around with the str to check for each ascii value

String str="have a look yourself";
char chr;
int ascii,length=str.length();

for(int i=0;i<length;i++){
out.println("Char at ("+i+") : "+chr+" ---> "+ascii);

The moon light is pouring down on my bedside<br />Like white frost spreading on the ground<br />I look up the bright round moon in the sky<br />And lower my head thinking of my dear hometown
Consider Paul's rocket mass heater.
subject: how to print ASCII value of a number ?
jQuery in Action, 3rd edition