/*
* 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)