Browse DevX
Sign up for e-mail newsletters from DevX

Tip of the Day
Language: Java Language
Expertise: Beginner
Apr 8, 1997



Building the Right Environment to Support AI, Machine Learning and Deep Learning

How do I get Java to return the ASCII value of a character?

How do I get Java to return the ASCII value of a character?

Java characters are encoded using Unicodes, two-byte codes that enable coding of non-Roman characters. Fortunately, Unicodes for Roman characters have the form 0x00yz where yz is the usual ASCII code.

Second, due to Java's stricter typing discipline, the char type is distinct from the numeric types, hence an explicit cast is required to convert a char to an int.

Here's a sample program that allows users to inspect the ASCII codes of characters contained in a command line argument:

public class CharTest {

   public static void main(String[] args) {
      int len = args[0].length();
      for(int i = 0; i < len; i++) {
         char next = args[0].charAt(i);
         System.out.println("Unicode for " + next + " = " + (short)next);
DevX Pro
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date