Solve the Problem of socket communication between Java and VB and socket communication between Java and C/C ++/OC

Source: Internet
Author: User
Tags url decode

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.

 

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.