java tutorial - Java Strings getBytes() method - java programming - learn java - java basics - java for beginners



Java strings getbytes

Learn Java - Java tutorial - Java strings getbytes - Java examples - Java programs

Description

The method getBytes () in Java has two forms:

  • getBytes (String charsetName) - it encodes this string into a byte sequence using charsetName (encoding), stores the result in a new byte array.
  • getBytes () - it encodes this string into a byte sequence, by default using the charset platform, stores the result in a new byte array.

Syntax

The syntax of the method is:

public byte[] getBytes(String charsetName) throws UnsupportedEncodingException

or

public byte[] getBytes()
click below button to copy the code. By - java tutorial - team

Options

Detailed information about the parameters:

  • charsetName - the name of the supported encodings.

Return value

  • In Java, getBytes () returns the resultant byte array.

Sample Code

import java.io.*;
public class Test {

   public static void main(String args[]) {
      String Str1 = new String("Welcome to wikitechy.com");
      try {
         String Str2 = new String(Str1.getBytes( "UTF-8" ));
         System.out.println("Returned Value " + Str2 );
         Str2 = new String (Str1.getBytes( "ISO-8859-1" ));
         System.out.println("Returned Value " + Str2 );
      } catch ( UnsupportedEncodingException e) {
         System.out.println("Unsupported character set");
      }
   }
}
click below button to copy the code. By - java tutorial - team

Output

Returned Value Welcome to wikitechy.com
Returned Value Welcome to wikitechy.com

Related Searches to Java Strings getBytes() method