Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

how to print ASCII value of a number ?  RSS feed

 
Bin Maths
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1511
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just cast it to an int.
ex:
char myChar = 'A';
System.out.println(myChar + "" + (int)myChar);

Bosun
 
Bin Maths
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, it was that simple?
regards.
 
Junilu Lacar
Sheriff
Posts: 10878
158
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Mirai Chelean
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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);
}
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!