Home Java Java-conversion Convert a Character into the ASCII Format

Related Tutorials


 
 

Share on Google+Share on Google+

Convert a Character into the ASCII Format

Advertisement
In this section, you will learn to convert a character data into the ASCII format.

Convert a Character into the ASCII Format

     

In this section, you will learn to convert a character data into the ASCII format. The java.lang package provides the functionality to convert the  character data into the ASCII format

Description of Code:

The following program helps in converting a character data into ASCII. Create a class "CharToASCII". This program takes a character at the console . It fragments the given string according to its positions with the help of charAt() method. This method returns the character at the specified index. Type casting ((int) c) is done to convert the character into an ASCII. 

Here is the code of this program:

import java.io.*;
import java.lang.*;

  public class CharToASCII{
  public static void main(String args[]) throws IOException{
  BufferedReader buff = 
  new 
BufferedReader(new InputStreamReader(System.in));
  System.out.println("Enter the char:");
  String str = buff.readLine();
  for int i = 0; i < str.length(); ++i ){
  char c = str.charAt(i);
  int j = (intc;
  System.out.println("ASCII OF "+c +" = " + j + ".");
  }
  }
  }

Download this program:

Output of this program.

C:\corejava>java CharToASCII
Enter the char:
A
ASCII OF A = 65.
C:\corejava>
Advertisements

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: June 15, 2007

Related Tutorials

Discuss: Convert a Character into the ASCII Format   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:4
s
December 12, 2011
sss

Nice Program on ascii
shiva narayana
August 11, 2012
java

i have these in my ascii array int ascii[]={'0','9','79','128','48','1','246','0','12','255','0','78','1','0','56','78','246','255','13','28','133','1','255','246','28','127','33','94','199','8','255','94','99','255','16','161','56','198','255','161','9','254','128','226','122','114','255','9','1','254','0','12','1','0','227','0','0','8','128','78','1','48','246','0'}; but while converting them to character for(int i=0;i<64;i++){ char c2 = (char) ascii[i]; ciphdata = ciphdata + c2; } i get result as 84 bit characters "**the problem is with array values it doesn't display character values between 0-32 ascii range values**" please help me out of this
shiva narayana
August 11, 2012
java

what about conversion of ascii to character if ascii= ( 0 - 32 ) range then we dont get any output please help me out!
Aasif Ali
July 26, 2013
char to ASCII

thanx alot for the post...
DMCA.com