String GetBytes(String CharsetName)



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 charsetName): It programs the String into a chain of bytes adopting the specified charset and return the array of those bytes.


Here is the syntax of this method:

public byte[] getBytes(String charsetName) throws UnsupportedEncodingException


Here is the detail of parameters −

  • charsetName − the name of a supported charset.

Return Value

  • This method returns the resulting byte array.


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:


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

Here at Intellinuts, we have created a complete Java tutorial for Beginners to get started in Java.