Java. lang. String. getBytes (String charsetName) Method Instance, string. getbytes
Java. lang. String. getBytes (String charsetName)The encoding method uses the byte sequence of the specified character set and stores the result to a new byte array.
Statement
Below isJava. lang. String. getBytes ()Method Declaration
public byte[] getBytes(String charsetName) throws UnsupportedEncodingException
Parameters
Return Value
This method returns the byte array.
Exception
Example
The following example shows how to use java. lang. String. getBytes.
package com.yiibai;import java.lang.*;public class StringDemo { public static void main(String[] args) throws Exception { // string with numbers and some special characters String str = "!$0123@"; // byte array with charset byte bval[] = str.getBytes("UTF8"); // prints the byte array for (int i = 0; i < bval.length; i++) { System.out.println(bval[i]); } }}
Let's compile and run the above program, which will produce the following results:
33364849505164