Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Java Java-conversion Convert Char To Byte

Related Tutorials


 
 

Share on Google+Share on Google+

Convert Char To Byte

Advertisement
This section illustrate the conversion from char to Byte. Here, we are going to convert a char type variable into byte type variable .

Convert Char To Byte

     

This section illustrate the conversion from char to Byte. Here, we are going to convert a char type variable into byte type variable . The following program helps you to convert from char to byte by providing the complete source code with program description.

Code Description:  

This program also defines the charAt() method used to find the character at the specified position. First of all, define the class named "CharToByte" for java component. The program reads an input string from the command line and stores it in a string, read the character at the specified position using the charAt() method and store it in a character type variable, convert this variable into byte by typecasting and then print the value at the console.

Here is the code of this program:

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

public class CharToByte {
  public static void main(String[] argsthrows IOException{
  BufferedReader buff = 
  new 
BufferedReader(new InputStreamReader(System.in));
  System.out.println("Enter a char:");
  String str = buff.readLine();
  char c = str.charAt(4);
  byte bValue = (byte)c;
  System.out.println("Byte is:=" + bValue);
  }
}

Download this program:

Output this program.

C:\corejava>java CharToByte
Enter a char :
roseindia
Byte is:=105
C:\corejava>
Advertisement

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: July 3, 2007

Related Tutorials

Discuss: Convert Char To Byte  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:4
Java Programmer
April 4, 2011
This is totally wong

This will only work if the representation of the character is a single byte. If the character is a unicode, multi-byte character, it won't work.
shakeel
April 4, 2012
computer

please tell me that what is numerical relation b/t character & byte,and tell me is it both the same,characet as well as byte,please,thanks
naresh
July 2, 2012
java

how to convert string to byte in .txt file?
vaishnavi deshmukh
July 12, 2012
corejava programming

char to byte conversion is possible or not???
DMCA.com