String-to-byte array and byte-array-to-string conversion (encoding and decoding issues)

Source: Internet
Author: User
Tags array to string

/*
* String (byte[] bytes, string charsetname): Decodes a byte array by the specified character set
* byte[] GetBytes (string charsetname): Encodes a string into a byte array using the specified character set
*
* Code: To see the understanding of the changes can not understand
* String--byte[]
*
* Decode: Turn what you don't understand into something you can understand.
* byte[]--String
*
* Example: Spy War Movie (Telegram, Telegram)
*
* Code table: Small Book
* Character value
*
* To send a text:
* I'll see you in the old place tonight *
*
* Send side: Today--value--binary--Send out
* Receiver: Receive-binary--decimal--value--character--now
*
* I'll see you in the old place tonight *
*
* Coding problem is simple, as long as the encoding and decoding format is consistent.

*/

Package IO. Encoding and decoding; import Java.io.unsupportedencodingexception;import Java.util.arrays;public class Stringdemo {public static void Main (string[] args) throws Unsupportedencodingexception {String s = "Hello";//string--byte[]//byte[] bys = S.get Bytes ();//[-28, -67, -96, -27, -91, -67]//byte[] bys = s.getbytes ("GBK");//[-60, -29, -70, -61]byte[] bys = s.getbytes ("UT F-8 ");//[-28,-67,-96,-27,-91, -67]//so the default encoding is UTF-8SYSTEM.OUT.PRINTLN (" string to byte array: "+arrays.tostring (bys));//decoding// Byte[]---string//string ss = new string (bys);//Hello//string ss = new String (bys, "GBK");//Raccoon ã ソstring SS = new String (bys, "UTF -8 ");//hello//So the default decoding of M is Utf-8system.out.println (" byte array to string: "+ss);}}


String-to-byte array and byte-array-to-string conversion (encoding and decoding issues)

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.