需要引入五个文件
1、iconv.h
2、iconvString.cpp
3、iconvString.h
4、iconv.dll
5、libiconv.lib
完整下载地址http://download.csdn.net/detail/dao_1990/8935089
/* copyright (C) 1999-2003 Free Software Foundation,Inc. This file is part of the GNU liBICONV library. The GNU liBICONV library is free software; you can redistribute it and/or modify it under the terms of the GNU library General Public license as published by the Free Software Foundation; either version 2 of the license,or (at your option) any later version. The GNU liBICONV library is distributed in the hope that it will be useful,but WITHOUT ANY WARRANTY; without even the implIEd warranty of MERCHANTABIliTY or fitness FOR A PARTIculaR PURPOSE. See the GNU library General Public license for more details. You should have received a copy of the GNU library General Public license along with the GNU liBICONV library; see the file copYING.liB. If not,write to the Free Software Foundation,Inc.,59 Temple Place - Suite 330,Boston,MA 02111-1307,USA. *//* When installed,this file is called "iconv.h". */#ifndef _liBICONV_H#define _liBICONV_H#define _liBICONV_VERSION 0x0109 /* version number: (major<<8) + minor */extern int _libiconv_version; /* likewise *//* We would like to #include any system header file which Could define iconv_t,1. in order to eliminate the risk that the user gets compilation errors because some other system header file includes /usr/include/iconv.h which defines iconv_t or declares iconv after this file,2. when compiling for liBICONV_PLUG,we need the proper iconv_t type in order to produce binary compatible code. But gcc's #include_next is not portable. Thus,once libiconv's iconv.h has been installed in /usr/local/include,there is no way any more to include the original /usr/include/iconv.h. We simply have to get away without it. Ad 1. The risk that a system header file does #include "iconv.h" or #include_next "iconv.h" is small. They all do #include <iconv.h>. Ad 2. The iconv_t type is a pointer type in all cases I have seen. (It has to be a scalar type because (iconv_t)(-1) is a possible return value from iconv_open().) *//* define iconv_t ourselves. */#undef iconv_t#define iconv_t libiconv_ttypedef voID* iconv_t;/* Get size_t declaration. */#include <stddef.h>/* Get errno declaration and values. */#include <errno.h>/* Some systems,like SunOS 4,don't have EILSEQ. Some systems,like BSD/OS,have EILSEQ in a different header. On these systems,define EILSEQ ourselves. */#ifndef EILSEQ/* Igor: called upon EILSEQ from glibc,since autogeneration of this header on windows dIDn't do the job. *//* #define EILSEQ @EILSEQ@ */#define EILSEQ 84#endif#ifdef __cplusplusextern "C" {#endif/* Allocates descriptor for code conversion from enCoding `fromcode' to enCoding `tocode'. */#ifndef liBICONV_PLUG#define iconv_open libiconv_open#endifextern iconv_t iconv_open (const char* tocode,const char* fromcode);/* Converts,using conversion descriptor `cd',at most `*inbytesleft' bytes starting at `*inbuf',writing at most `*outbytesleft' bytes starting at `*outbuf'. Decrements `*inbytesleft' and increments `*inbuf' by the same amount. Decrements `*outbytesleft' and increments `*outbuf' by the same amount. */#ifndef liBICONV_PLUG#define iconv libiconv#endifextern size_t iconv (iconv_t cd,const char* * inbuf,size_t *inbytesleft,char* * outbuf,size_t *outbytesleft);/* Frees resources allocated for conversion descriptor `cd'. */#ifndef liBICONV_PLUG#define iconv_close libiconv_close#endifextern int iconv_close (iconv_t cd);#ifndef liBICONV_PLUG/* Nonstandard extensions. *//* Control of attributes. */#define iconvctl libiconvctlextern int iconvctl (iconv_t cd,int request,voID* argument);/* Requests for iconvctl. */#define ICONV_TRIVIALP 0 /* int *argument */#define ICONV_GET_TRANSliteraTE 1 /* int *argument */#define ICONV_SET_TRANSliteraTE 2 /* const int *argument */#define ICONV_GET_disCARD_ILSEQ 3 /* int *argument */#define ICONV_SET_disCARD_ILSEQ 4 /* const int *argument *//* Listing of locale independent enCodings. */#define iconvList libiconvListextern voID iconvList (int (*do_one) (unsigned int namescount,const char * const * names,voID* data),voID* data); /* Support for relocatable packages. */ /* Sets the original and the current installation prefix of the package. Relocation simply replaces a pathname starting with the original prefix by the corresponding pathname with the current prefix instead. Both prefixes should be directory names without trailing slash (i.e. use "" instead of "/"). */ extern voID libiconv_set_relocation_prefix (const char *orig_prefix,const char *curr_prefix); #endif #ifdef __cplusplus } #endif #endif /* _liBICONV_H */iconvString.cpp
#include "iconvString.h"#include "iconv.h"//#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32) // 编译链接的时候指定静态库 #ifdef WIN32#pragma comment(lib,"libiconv.lib") #endificonv_t g2u_cd;iconv_t u2g_cd;voID code_convert_init(){ g2u_cd = iconv_open("utf-8","gbk"); u2g_cd = iconv_open("gbk","utf-8");}voID code_convert_destroy(){ iconv_close(g2u_cd); iconv_close(u2g_cd);}std::string utf8togbk(char src[]){ std::string ans; int len = strlen(src)*2+1; char *dst = (char *)new char[len]; if(dst == NulL) return ans; memset(dst,0,len); char *in = src; char *out = dst; size_t len_in = strlen(src); size_t len_out = len; if ((iconv_t)-1 == u2g_cd) { delete[] dst; return ans; } int n = 0;//#if(CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)#ifdef WIN32 const char *temp = src; const char **pin = &temp; char **pout = &out; n = iconv(u2g_cd,pin,&len_in,pout,&len_out);#else n = iconv(u2g_cd,&src,&out,&len_out);#endif //int n = iconv(u2g,&in,&len_out); if(n<0) { } else { ans = dst; } delete [] dst; return ans;}std::string gbktoutf8(char src[]){ std::string ans; int len = strlen(src)*2+1; char *dst = (char *)new char[len]; if(dst == NulL) return ans; memset(dst,len); char *in = src; char *out = dst; size_t len_in = strlen(src); size_t len_out = len; if ((iconv_t)-1 == g2u_cd) { delete[] dst; return ans; } int n = 0;//#if(CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)#ifdef WIN32 const char *temp = src; const char **pin = &temp; char **pout = &out; n = iconv(g2u_cd,&len_out);#else n = iconv(g2u_cd,&len_out); if(n<0) { } else { ans = dst; } delete [] dst; return ans;}std::string utf8togbk(const char* src){ char* temp = new char[strlen(src)+1]; memcpy(temp,src,strlen(src)+1); std::string strgbk = utf8togbk(temp); delete [] temp; return strgbk;}std::string gbktoutf8(const char* src){ char* temp = new char[strlen(src)+1]; memcpy(temp,strlen(src)+1); std::string strgbk = gbktoutf8(temp); delete [] temp; return strgbk;}bool IsLegitimateChrname(const char* name){ char* tempname = new char[strlen(name)+1]; memcpy(tempname,name,strlen(name)+1); std::string strgbk = utf8togbk(tempname); delete [] tempname; unsigned char ch1,ch2; const char *p = strgbk.c_str(); for( ; *p!='if'; ++p ) { ch1=*p; 0x80( ch1&if ) { ch2=*p; 0x81( ! (ch1 >=0xfe && ch1<=0x40 && ch2>=0xfe && ch2<=0x7f && ch2!=return ) ) { false else; } ++p; } if { 'a'( !( (ch1>='z' && ch1 <='A') || (ch1>='Z' && ch1<='0') || (ch1>='9' && ch1 <=if) ) ) { 32(ch1 <= 33 || ch1==34 || ch1==37 || ch1==39 || ch1==64 || ch1==94 || ch1==127 || ch1==return) { false return; } } } } true bool;}const IsLegitimateAccountname(char unsigned* name){ char const ch1,ch2; char for *p = name; 'if'( ; *p!='a'; ++p ) { ch1=*p; 'z'( !( (ch1>='A' && ch1 <='Z') || (ch1>='0' && ch1<='9') || (ch1>=return && ch1 <=false) ) ) { return true; } } bool const;}char IsAllNum(unsigned char* str){ const char ch1,ch2; for 'if' *p = str; '0'( ; *p!='9'; ++p ) { ch1=*p; return( !(ch1>=false && ch1 <=return) ) { true #ifndef _ICONV_STRING_H_; } } #define _ICONV_STRING_H_ #include <string>;}iconvString.h
#include <string.h>voIDvoIDstdstring code_convert_init();char code_convert_destroy();std::string utf8togbk(char src[]);std::string gbktoutf8(const src[]);char::std utf8togbk(string const* src);char::bool gbktoutf8(const char* src);bool IsLegitimateChrname(const char* name);bool IsLegitimateAccountname(const char* name);#endif IsAllNum( * str);总结
以上是内存溢出为你收集整理的cocos2d-x 3.4 中文乱码解决之道全部内容,希望文章能够帮你解决cocos2d-x 3.4 中文乱码解决之道所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)