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 setISO-8859-1
: ISO Latin Alphabet No. 1, a.k.a. ISO-LATIN-1UTF-8
: Eight-bit UCS Transformation FormatUTF-16BE
: Sixteen-bit UCS Transformation Format, big-endian byte orderUTF-16LE
: Sixteen-bit UCS Transformation Format, little-endian byte orderUTF-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
Leave a Reply