aspose file tools*
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
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
Author

how to print ASCII value of a number ?

Bin Maths
Greenhorn

Joined: May 29, 2001
Posts: 5
Hello,
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.?
Regards
Bosun Bello
Ranch Hand

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

Bosun


Bosun (SCJP, SCWCD)
So much trouble in the world -- Bob Marley
Bin Maths
Greenhorn

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

Joined: Feb 26, 2001
Posts: 4699
    
    7

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

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

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++){
chr=str.charAt(i);
ascii=(int)chr;
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to print ASCII value of a number ?