Java.lang.String.getBytes (String CharsetName) Method instance

Source: Internet
Author: User

The java.lang.String.getBytes (string charsetname) method encodes this String using a sequence of bytes for the specified character set and stores the result in a new byte array.

Statement

The following is a declaration of the Java.lang.String.getBytes () method

Publicbyte[] getBytes(String charsetname)throws  Unsupportedencodingexception        
Parameters
    • CharSet-This is the name of a supported character set.

return value

This method returns the resulting byte array.

Abnormal
    • Unsupportedencodingexception--If the specified character set is not supported.

Example

The following example shows the use of the Java.lang.String.getBytes () method.

PackageCom.Yiibai;ImportJava.Lang.*;Public Class Stringdemo { Public Static voidMain(String[]Args) Throws Exception { String with numbers and some special characters StringStr= "[Email protected]"; byte array with CharSet ByteBval[] =Str.GetBytes("UTF8"); Prints the byte array for  (int I = 0; I < Bval.; I++)  { system.. Printlnbval[i } }}   

Let's compile and run the above program, which will produce the following results:

A      .

Java.lang.String.getBytes (String CharsetName) Method instance

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.