An analysis of a bug in the analysis and solution of C + + and Java Chinese garbled problem

Source: Internet
Author: User

An analysis of a bug in the analysis and solution of C + + and Java Chinese garbled problem

Dionysoslai ([email protected]) 2014/10/21

In the previous several blog "about C + + and Java Chinese garbled problem analysis and resolution," The address is as follows: http://blog.csdn.net/dionysos_lai/article/details/38389765. This paper introduces in detail the reasons why Chinese garbled characters occur in C + + and Java data transmission, and puts forward the appropriate solution. Here's how:

int Ccdirector::gbktoutf8 (std::string &gbkstr) {iconv_t ICONVH;          ICONVH = Iconv_open ("Utf-8", "gb2312");          if (ICONVH = = 0) {return-1;          } const char* Strchar = Gbkstr.c_str ();            Const char** PIN = &strChar;          size_t strlength = Gbkstr.length ();          char* outbuf = (char*) malloc (strlength*4);          char* pbuff = outbuf;          memset (OUTBUF,0,STRLENGTH*4);      size_t outlength = strlength*4; <span style= "color: #ff6666;" ><strong> #if (Cc_target_platform = = Cc_platform_win32) if ( -1 = = Iconv (iconvh,pin,&strlength,&outbuf              , &outlength)) {iconv_close (ICONVH);          return-1; } #else if ( -1 = = Iconv (ICONVH, (char * *) pin,&strlength,&outbuf,&outlength)) {iconv_close (IC              ONVH);          return-1; } #endif </strong></span> gbkstr = PBuff;          Iconv_close (ICONVH);      return 0;   }

This method, in the two months of the project, after a number of game detection, no big problem. Not enough later, when passing a special string, a serious bug was found. This particular string is the null character, or "". At this time, pass over is garbled. This is true for both Win32 and Android platforms.

The problem is that it appears in the following section of code:

size_t strlength    = Gbkstr.length ();      char* outbuf        = (char*) malloc (strlength*4);      char* Pbuff         = outbuf;  
When we pass a null character (""), the Strlength value is 0, and the following memory assignment will obviously cause an error. Therefore, for the null character "", to do further processing. The code after processing is as follows:
int XtcUtils:    : GBKToUTF8 (std::string &gbkstr) {iconv_t ICONVH;    ICONVH = Iconv_open ("Utf-8", "gb2312");    if (ICONVH = = 0) {return-1;    } const char* Strchar = Gbkstr.c_str ();    Const char** PIN = &strChar;    size_t strlength = Gbkstr.length (); if (0 = = strlength)///< Special case, Gbkstr for "", Strlength = 0 o'clock, transcoding will have garbled {gbkstr = "";}    else{char* outbuf = (char*) malloc (strlength*4);    char* pbuff = outbuf;    memset (OUTBUF,0,STRLENGTH*4);    size_t outlength = strlength*4;    #if (Cc_target_platform = = Cc_platform_win32) if ( -1 = = Iconv (iconvh,pin,&strlength,&outbuf,&outlength)) {    Iconv_close (ICONVH);    return-1;    } #elseif ( -1 = = Iconv (ICONVH, (char * *) pin,&strlength,&outbuf,&outlength)) {iconv_close (ICONVH);    return-1; } #endifgbkStr = Pbuff;    } iconv_close (ICONVH);    return 0; }

The above is the detailed code, there are excerpts from the previous text code of the students, hope to correct the next, do not appear serious bug.




An analysis of a bug in the analysis and solution of C + + and Java Chinese garbled problem

Related Article

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.