getBytes() Method In Java Example
Chapter:
String Handling
Last Updated:
15-07-2016 11:13:13 UTC
Program:
/* ............... START ............... */
import java.io.UnsupportedEncodingException;
public class JavaStringGetBytes {
public static void main(String args[]) {
String Str1 = new String("Welcome to JavaScan.com");
try {
byte[] Str2 = Str1.getBytes();
System.out.println("Value:" + Str2);
Str2 = Str1.getBytes("UTF-8");
System.out.println("Value:" + Str2);
Str2 = Str1.getBytes("ISO-8859-1");
System.out.println("Value:" + Str2);
} catch (UnsupportedEncodingException e) {
System.out.println("Unsupported character set");
}
}
}
/* ............... END ............... */
Output
Notes:
-
getBytes() encodes this String into a sequence of bytes using the platform's default charset, storing the result into a new byte array.
Tags
getBytes(), Java