3:53 pm - Thursday March 24, 5921

String Example Program for getBytes(String charsetName)

CANDID INDUSTRIAL TRAINING -- CHENNAI
Corporate Training for Experienced Candidate
Struts | Hibernate | Spring | Java / J2EE
SOAP | RestFull | Design Pattern | more...
Ph: +91 72000 69003
137b, 2nd st, shanthi nagar, Chrompet, Chennai -600044
/**
@AUTHOR: candid java
@DESCRIPTION: coding for  convert the string into given characterset byte formate
*/

import java.io.*;
public class GetBytesCharset
{
public static void main(String args[])
{
try
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("enter the string:");
String s=br.readLine();
byte[] b=s.getBytes("ASCII");// converting the string into ASCII formate and store into the byte array
System.out.println("the ASCII code for the given string="+b);
for(int i=0;i<b.length;i++)//convert each byte into character
{
char c=(char)b[i];
System.out.println("the equal ASCII code for"+" "+c+"="+b[i]);
}
}
catch(Exception e)
{
System.out.println(e);
}
}
}

OUTPUT:
enter the string
MY JAVA
the ASCII code for the given string=[B@42e816
the equal ASCII code for M=77
the equal ASCII code for Y=89
the equal ASCII code for =32
the equal ASCII code for J=74
the equal ASCII code for A=65
the equal ASCII code for V=86
the equal ASCII code for A=65

BACK

Filed in: Z-A All Java Codes

Comments are closed.