beginnersbook.com

  • Home
  • All Tutorials
    • Learn Servlet
    • Learn JSP
    • Learn JSTL
    • Learn C
    • Learn C++
    • Learn MongoDB
    • Learn XML
    • Learn Python
    • Learn Perl
    • Learn Kotlin
  • Core Java
  • OOPs
  • Collections
  • Java I/O
  • JSON
  • DBMS

Java – String getBytes() Method example

By Chaitanya Singh | Filed Under: String handling

The getBytes() method encodes a given String into a sequence of bytes and returns an array of bytes. The method can be used in below two ways:

public byte[] getBytes(String charsetName): It encodes the String into sequence of bytes using the specified charset and return the array of those bytes. It throws UnsupportedEncodingException – If the specified charset is not supported.
public byte[] getBytes(): It encodes the String using default charset method.

Example: getBytes() method

import java.io.*;
public class GetBytesExample{
   public static void main(String args[]){
       String str = new String("Hello");
       byte[] array1 = str.getBytes();
       System.out.print("Default Charset encoding:");
       for(byte b: array1){
           System.out.print(b);
       }
       System.out.print("\nUTF-16 Charset encoding:");
       try{
             byte [] array2 = str.getBytes("UTF-16");
             for(byte b1: array2){
                System.out.print(b1);
             }
             byte [] array3 = str.getBytes("UTF-16BE");
             System.out.print("\nUTF-16BE Charset encoding:");
             for(byte b2: array3){
                System.out.print(b2);
             }
        }catch(UnsupportedEncodingException ex){
             System.out.println("Unsupported character set"+ex);
        }
   }	
}

Output:

Default Charset encoding:72101108108111
UTF-16 Charset encoding:-2-10720101010801080111
UTF-16BE Charset encoding:0720101010801080111

In the above example we have done encoding using charset UTF -16 and UTF - 16BE, there are many other standard charset like:

  • US-ASCII: Seven-bit ASCII, a.k.a. ISO646-US, a.k.a. the Basic Latin block of the Unicode character set
  • ISO-8859-1: ISO Latin Alphabet No. 1, a.k.a. ISO-LATIN-1
  • UTF-8: Eight-bit UCS Transformation Format
  • UTF-16BE: Sixteen-bit UCS Transformation Format, big-endian byte order
  • UTF-16LE: Sixteen-bit UCS Transformation Format, little-endian byte order
  • UTF-16: Sixteen-bit UCS Transformation Format, byte order identified by an optional byte-order mark.

Reference:

http://docs.oracle.com/javase/7/docs/api/java/nio/charset/Charset.html

Enjoyed this post? Try these related posts

  1. Java String replace(), replaceFirst() and replaceAll() method
  2. Java – String copyValueOf() Method example
  3. Java – String getChars() Method example
  4. Java String compareToIgnoreCase() Method example
  5. Java String contains() method explained with examples
  6. Java – String toLowerCase() and toUpperCase() Methods

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Java Tutorials

  • Learn Java
  • OOPs Concepts
  • Java Collections

Java String

  • Java String

Java String Methods

  • String charAt()
  • String compareTo()
  • String compareToIgnoreCase()
  • String contains()
  • String concat()
  • substring
  • String valueOf()
  • String startsWith()
  • String equals()
  • String format()
  • String endsWith()
  • String indexOf()
  • String lastIndexOf()
  • String length()
  • String replace()
  • String split()
  • String trim()
  • String intern()
  • String isEmpty()
  • String matches()
  • String regionMatches()
  • String contentEquals()
  • String toCharArray()
  • String getBytes()
  • String join()
  • String getChars()
  • String copyValueOf()

Recently Added..

  • JSON Tutorial
  • Java Regular Expressions Tutorial
  • Java Enum Tutorial
  • Java Annotations Tutorial

Copyright © 2012 – 2021 BeginnersBook . Privacy Policy . Sitemap