This article analyzes a bug in "Analysis and Solution of Chinese garbled characters in c ++ and java", Jiang Zhongyi

Source: Internet
Author: User

This article analyzes a bug in "Analysis and Solution of Chinese garbled characters in c ++ and java", Jiang Zhongyi

A bug analysis in "Analysis and Solution to Chinese garbled characters in c ++ and java"

DionysosLai (906391500@qq.com)

In the previous blogs about how to analyze and solve Chinese garbled characters in c ++ and java, the address is http://blog.csdn.net/dionysos_lai/article/details/38389765. This article describes in detail why Chinese garbled characters occur during data transmission in c ++ and java, and provides appropriate solutions. The method is as follows:

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(iconvH);              return -1;          }   #endif</strong></span>           gbkStr              =   pBuff;          iconv_close(iconvH);          return 0;      }  

This method has been detected by multiple games in the two months, and there is no major problem. Later, a serious bug was found when a special string was passed. This special string is a null character, that is "". In this case, garbled characters are passed. This is the case on both Win32 and Android platforms.

The problem occurs in the following code:

size_t strLength    = gbkStr.length();      char* outbuf        = (char*)malloc(strLength*4);      char* pBuff         = outbuf;  
When we pass a null character ("") and the strLength value is 0, the following memory assignment will obviously cause an error. Therefore, we need to further process the null character. The processed code is as follows:
Int XtcUtils: GBKToUTF8 (std: string & gbkStr) {iconv_t iconku; iconku = iconv_open ("UTF-8", "gb2312"); if (iconku = 0) {return-1;} const char * strChar = gbkStr. c_str (); const char ** pin = & strChar; size_t strLength = gbkStr. length (); if (0 = strLength) // <in special cases, if gbkStr is "" And strLength = 0, the transcoding will contain garbled characters {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 (iconvl, pin, & strLength, & outbuf, & outLength )) {iconv_close (iconvl); return-1 ;}# elseif (-1 = iconv (iconvl, (char **) pin, & strLength, & outbuf, & outLength )) {iconv_close (iconvl); return-1 ;}# endifgbkStr = pBuff;} iconv_close (iconvl); return 0 ;}

The above is the detailed code. Some students who have excerpted the previous Code hope to correct the code and avoid serious bugs.





Java Chinese garbled

Because UTF-8 is encoded in linux, you may encounter garbled characters when using windos in linux. Similarly, if you use gb in a linux environment, it will be garbled, which is determined by the operating system. Therefore, you must use gb for windows. If you change it to another one, it will be garbled. Not supported by the operating system.

Chinese garbled java files

Select your file -- Right-click -- properties -- text file encoding there is a default and other here select other and then select UTF-8 (your default may be ISO-8859-1)
Then you change the garbled characters in the file back to Chinese and save it ~ This time, no garbled characters ~

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.