Question:
How do I get Java to return the ASCII value of a character?
Answer:
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 typeis distinct from the numeric types, hence an explicit cast is requiredto convert a char to an int.
Here’s a sample program that allows users to inspect the ASCIIcodes 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); } }}
Charlie has over a decade of experience in website administration and technology management. As the site admin, he oversees all technical aspects of running a high-traffic online platform, ensuring optimal performance, security, and user experience.
























