Solve the problem of Chinese character string encoding during socket communication between Java and VB and C/C ++/OC
I encountered a Java encoding character set using UTF-8 encoding characters using 2 + characters I testedCodeAs follows:
System. out. println ("UTF-8: A->" + "". getbytes ("UTF-8 "). length); system. out. println ("UTF-8: Hi->" + "hi ". getbytes ("UTF-8 "). length); system. out. println ("gb2312: A->" + "". getbytes ("gb2312 "). length); system. out. println ("gb2312: Hi->" + "hi ". getbytes ("gb2312 "). length); system. out. println ("UNICODE: A->" + "". getbytes ("Unicode "). length); system. out. println ("UNICODE: Hi->" + "hi ". getbytes ("Unicode "). length); // Test Results // UTF-8: A-> 1 // UTF-8: Hi-> 3 // gb2312: A-> 1 // gb2312: hi-> 2 // UNICODE: A-> 4 // UNICODE: Hi-> 4
In addition, if the socket is used to transmit data in bytes, the problem may also occur. I have carefully studied Java, C ++, VB, OC, and so on. (PS: I only know these languages, but some do not. But many languages are developed based on C)
Java Byte ranges from-127 to 128, while C, C ++, VB, and OC are 0-255.
So what I think of is the simplest and most common method, so that I can avoid a lot of detours. What kind of high turn to low, the status turns to high, this doesn't work, it's called a cry (before I found this, I have learned a lot about it, and I will share it here .)
To put it bluntly: Before Android, I used javaee Java Web, so I was familiar with urldecoder. I tried to convert the URL encoding format !! Finally
The Java urldecoder and urlencoder URL encoding and decoding are as follows:
// Send socket data and write out dos = new dataoutputstream (msocket. getoutputstream (); // dos. writeutf (S. trim () + "\ n"); dos. write (urlencoder. encode (S, "gb2312 "). getbytes (); dos. flush (); // receives socketmsocket. getinputstream (). read (receive); string Ss = new string (receive); // Ss = ss. substring (0, SS. lastindexof ("% 02"); string data = urldecoder. decode (SS, "gb2312"); system. out. println (data );
C, C ++, VB, oc I will not post code to these non-professional users. For details, please google it. There should also be unicode url decode and encode encoding. Anyway, I am here to perform VB communication works in this way.
If you have a good method, you can share the idea.