Java string getbytes

Source: Internet
Author: User

Everyone knows that Java's Char is a double byte character, while string provides the getbytes function. What kind of form does the bytes array obtained by this function exist? Is the Double Byte Character order in an array?

Do a test

 

String ts1 = "112 ";
Byte [] ts2 = NULL;
Ts2 = ts1.getbytes ();
This output is 112 ASCII 49 49 50
Note that this overload method adds an encoding parameter. Try another one.

String tests1 = "112 ";
Byte [] tests2 = NULL;
Try {
Ts2 = ts1.getbytes ("UTF-8 ");
} Catch (unsupportedencodingexception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
}

Output or 494950

String tests1 = "112 ";
Byte [] tests2 = NULL;
Try {
Ts2 = ts1.getbytes ("UTF-16LE ");
} Catch (unsupportedencodingexception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
}

Output to 490 490 500

Check out the SDK ,--! I wrote it too clearly ..

byte[] getBytes()
Use the default Character Set of the platformStringEncode the data as a byte sequence and store the results in a new byte array.
 byte[] getBytes(Charset charset)
Use the given charsetStringEncode the data to the byte sequence and store the results to the new byte array.

The default charset of the system is GBK, while the Utf-8 uses variable Byte encoding and the Utf-16 uses two-byte fixed length encoding.

 

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.