Java String getBytes(Charset charset) Example
String class getBytes(Charset charset) method example
String class getBytes(Charset charset) method example. This example shows you how to use getBytes(Charset charset) method.This method Encodes this String into a sequence of bytes using the given charset, storing the result into a new byte array.
Here is the Code:-
/**
* @(#) GetBytes1string.java
*Program that Convert this String into bytes according to the specified character encoding, storing the result into a new byte array.
* @Version 01-May-2008
* @author Rose India Team
*/
public class GetBytes1string
{
public static void main(String[] args)
{
try
{
String str = "Welcome to RoseIndia.";
// Copy the contents of the String to a byte array using the ASCII encoding.
byte[] arr = str.getBytes("ASCII");
// Create a new String using the contents of the byte array.
String newStr = new String(arr);
// Display the contents of the byte array.
System.out.println("The new String equals \"" +newStr + "\"");
}
catch (Exception ex)
{
}
}
} |
null
|