Wednesday 18 January 2017

Java String getBytes() Method




The getBytes() Method:

  This method stores characters in an array of bytes. It converts the string into bytes according to the specified character encoding and stores the result in an array. This is an alternative to getChars() method.

 Syntax Of getBytes() Method:

  There are 3 variant of this method. The syntax of this method given bellow:

 public byte[] getBytes()
 public byte[] getBytes(Charset charset)
 public byte[] getBytes(String charsetName)throws UnsupportedEncodingException

 GetBytesTest.java

 public class GetBytesTest
 {
  public static void main(String args[])
  {
    String str=" Java Programing ";
    byte[] barr=str.getBytes();
    for(int i=0;i<barr.length;i++)
   {
    System.out.println(barr[i]);
    }
  }
  }


 Output is:
 65
 66
 67
 68
 69
 70


0 comments:

Post a Comment

Powered by Blogger.

Stats