Analysis and solution of rubbish problem in C + + and Java China
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. In this paper, we introduce the C + + and Java data transfer. Why the Chinese garbled reason, and put forward the appropriate solution.
Methods such as the following:
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 this two-month project. After a number of game tests, there is no big problem. Not enough later. When a special string is passed. A serious bug was found. This particular string is the null character, or "". Then. Pass over is garbled. Both on Win32 and Android platforms. This is the case.
The problem is now in the following 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. The following memory assignments will obviously cause errors.
Therefore, for the null character "", to do further processing. The processed code looks like the following:
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 have a serious bug.
Copyright notice: This article Bo Master original article. Blog, not reproduced without consent.
Analysis and solution of rubbish problem in C + + and Java China