logo

Java String GetBytes() Method


Show

Description

This technique encodes this String into a succession of bytes utilizing the platform's evasion charset, storing the effect into a fresh byte selection. The getBytes() technique encodes a known String into a run of bytes and revisits a display of bytes. The technique can be utilized in two ways: public byte[] getBytes(String charset name): It programs the String into a chain of bytes adopting the specified charset and return the array of those bytes.

Syntax

Here is the syntax of this method:

public byte[] getBytes()

Return Value

  • This method returns the resulting byte array.

Example

import java.io.*;
public class Test {

   public static void main(String args[]) {
      String Str1 = new String("Welcome to Inteliinuts.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");
      }
   }
}

This will produce the following result:

Output

Returned Value Welcome to Inteliinuts.com
Returned Value Welcome to Inteliinuts.com