• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

how to print ASCII value of a number ?

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 1514
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, it was that simple?
regards.
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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);
}
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic