Login | Register   
RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX

By submitting your information, you agree that devx.com may send you DevX offers via email, phone and text message, as well as email offers about other products and services that DevX believes may be of interest to you. DevX will process your information in accordance with the Quinstreet Privacy Policy.

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



Application Security Testing: An Integral Part of DevOps

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