How do I convert a string and a byte array in Java?

Source: Internet
Author: User

1, byte array converted to string

byte[] Bybuffer = new BYTE[20];
... ...
String strread = new string (Bybuffer);
Strread = string.copyvalueof (Strread.tochararray (), 0, bybuffer.length]);

2. Converting a string into a byte array

byte[] Bybuffer = new byte[200];
String strinput= "ABCDEFG";
Bybuffer= strinput.getbytes ();

Note: If the string contains Chinese, pay special attention to, under the Android system, the default is UTF8 encoding, a Chinese character equivalent to 3 bytes, only gb2312 the next Mandarin equivalent to 2 bytes. In this case, the following approaches may be adopted:

byte[] Bybuffer = new byte[200];
String strinput= "I am a string";
Bybuffer= strinput.getbytes ("gb2312");

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.