Scene:
1. When passing the byte[] array through the socket to Java, Utf-8 's byte array is converted to string, Java does not encounter 0 stops ending, but has been using the capacity of byte[].
So the string converted to Java needs to judge the byte value to be 0, and then intercept the array length.
public static int Searchbyte (byte[] data, byte value) {int size = data.length;for (int i = 0; i < size; ++i) {if (data [I] = = value) {return i;}} return-1;} public static void Main (string[] args) {byte[] info = new Byte[10];info[0] = 0x31;info[1] = 0x31;info[2] = 0;info[3] = 0x1 ; Info[4] = 0x32;info[5] = 0;try {string UTF8 = new String (info, "UTF-8");//1. This is the size of the array, not the 2,string even if encountering 0 will continue to load into String.//Loss Out: 10system.out.println ("" + utf8.length ());//1. The output is strange and does not output placeholders for invisible characters//output: 112system.out.println (UTF8); if ( Utf8.equalsignorecase ("112")) {//and 112 are not output. System.out.println ("It is same with 112");} byte[] Info1 = utf8.getbytes ("UTF-8");//1. Revert to byte[], the discovery data is not lost.//output: 10:0:1:50system.out.println ("info1:" + info1.length + ":" + info1[2] + ":" + info1[3] + ":" + I NFO1[4]);//1. So if you need to end the UTF8 byte array to 0, you need to judge 0 yourself and intercept. int offset = -1;//BinarySearch must be sorted in ascending order to be used, so it cannot be used.//offset = Arrays.binarysearch ( info, (byte) 0); offset = Searchbyte (info, (byte) 0); String Info2 = new String (info, 0, offset, "UTF-8");/output: 11system.out.println ("Info2:" + Info2);//output: 2system.out.println ("" + info2.length ());} catch (Unsupportedencodingexception e) {//TODO auto-generated catch Blocke.printstacktrace ();}}
[java]_[primary]_[UTF8 encoded byte[] An array of issues to be aware of when converting to string