cocos2dx 中文乱码解决之道

须要引入五个文件

1、iconv.h

2、iconvString.cpp

3、iconvString.h

4、iconv.dll

5、libiconv.lib

完整下载地址http://download.csdn.net/detail/dao_1990/8935089

iconv.h

/* 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_t
typedef 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 __cplusplus
extern "C" {
#endif /* Allocates descriptor for code conversion from encoding `fromcode' to
encoding `tocode'. */
#ifndef LIBICONV_PLUG
#define iconv_open libiconv_open
#endif
extern 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
#endif
extern 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
#endif
extern int iconv_close (iconv_t cd); #ifndef LIBICONV_PLUG /* Nonstandard extensions. */ /* Control of attributes. */
#define iconvctl libiconvctl
extern 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 libiconvlist
extern 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")
#endif iconv_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, &len_in, &out, &len_out);
#endif
//int n = iconv(u2g, &in, &len_in, &out, &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, 0, 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, pin, &len_in, pout, &len_out);
#else
n = iconv(g2u_cd, &src, &len_in, &out, &len_out);
#endif
//int n = iconv(u2g, &in, &len_in, &out, &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, src, 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!='\0'; ++p )
{
ch1=*p;
if( ch1&0x80 )
{
ch2=*p;
if( ! (ch1 >=0x81 && ch1<=0xfe && ch2>=0x40 && ch2<=0xfe && ch2!=0x7f ) )
{
return false;
}
++p;
}
else
{
if( !( (ch1>='a' && ch1 <='z') || (ch1>='A' && ch1<='Z') || (ch1>='0' && ch1 <='9') ) )
{
if(ch1 <= 32 || ch1==33 || ch1==34 || ch1==37 || ch1==39 || ch1==64 || ch1==94 || ch1==127)
{
return false;
}
} }
}
return true;
} bool IsLegitimateAccountName(const char* name)
{
unsigned char ch1,ch2;
const char *p = name;
for( ; *p!='\0'; ++p )
{
ch1=*p; if( !( (ch1>='a' && ch1 <='z') || (ch1>='A' && ch1<='Z') || (ch1>='0' && ch1 <='9') ) )
{
return false;
}
}
return true;
} bool IsAllNum(const char* str)
{
unsigned char ch1,ch2;
const char *p = str;
for( ; *p!='\0'; ++p )
{
ch1=*p; if( !(ch1>='0' && ch1 <='9') )
{
return false;
}
}
return true;
}

iconvString.h

#ifndef _ICONV_STRING_H_
#define _ICONV_STRING_H_ #include <string>
#include <string.h> void code_convert_init(); void code_convert_destroy(); std::string utf8togbk(char src[]); std::string gbktoutf8(char src[]); std::string utf8togbk(const char* src); std::string gbktoutf8(const char* src); bool IsLegitimateChrName(const char* name);
bool IsLegitimateAccountName(const char* name);
bool IsAllNum(const char* str); #endif

cocos2d-x 3.4 中文乱码解决之道的更多相关文章

  1. java中文乱码解决之道(九)-----总结

    乱码,我们前台展示的杀手,可能有些朋友和我的经历一样:遇到乱码先按照自己的经验来解决,如果没有解决就google,运气好一搜就可以解决,运气不好可能够你折腾一番了.LZ之所以写这个系列博客就是因为遇到 ...

  2. java中文乱码解决之道(二)-----字符编码详解:基础知识 + ASCII + GB**

    在上篇博文(java中文乱码解决之道(一)-----认识字符集)中,LZ简单介绍了主流的字符编码,对各种编码都是点到为止,以下LZ将详细阐述字符集.字符编码等基础知识和ASCII.GB的详情. 一.基 ...

  3. java中文乱码解决之道(七)-----JSP页面编码过程

    我们知道JSP页面是需要转换为servlet的,在转换过程中肯定是要进行编码的.在JSP转换为servlet过程中下面一段代码起到至关重要的作用. <%@ page language=" ...

  4. java中文乱码解决之道(二)—–字符编码详解:基础知识 + ASCII + GB**

    原文出处:http://cmsblogs.com/?p=1412 在上篇博文(java中文乱码解决之道(一)—–认识字符集)中,LZ简单介绍了主流的字符编码,对各种编码都是点到为止,以下LZ将详细阐述 ...

  5. java中文乱码解决之道(七)—–JSP页面编码过程

    我们知道JSP页面是需要转换为servlet的,在转换过程中肯定是要进行编码的.在JSP转换为servlet过程中下面一段代码起到至关重要的作用. <%@ page language=" ...

  6. java中文乱码解决之道(一)-----认识字符集

    沉寂了许久(大概有三个多月了吧),LZ"按捺不住"开始写博了! java编码中的中文问题是一个老生常谈的问题了,每次遇到中文乱码LZ要么是按照以前的经验修改,要么则是baidu.c ...

  7. java中文乱码解决之道(八)-----解决URL中文乱码问题

    我们主要通过两种形式提交向服务器发送请求:URL.表单.而表单形式一般都不会出现乱码问题,乱码问题主要是在URL上面.通过前面几篇博客的介绍我们知道URL向服务器发送请求编码过程实在是实在太混乱了.不 ...

  8. java中文乱码解决之道(一)—–认识字符集

    原文出处:http://cmsblogs.com/?p=1395 沉寂了许久(大概有三个多月了吧),LZ“按捺不住”开始写博了! java编码中的中文问题是一个老生常谈的问题了,每次遇到中文乱码LZ要 ...

  9. java中文乱码解决之道(四)-----java编码转换过程

    前面三篇博客侧重介绍字符.编码问题,通过这三篇博客各位博友对各种字符编码有了一个初步的了解,要了解java的中文问题这是必须要了解的.但是了解这些仅仅只是一个开始,以下博客将侧重介绍java乱码是如何 ...

随机推荐

  1. 洛谷 P1480 A/B Problem

    P1480 A/B Problem 题目描述 输入两个整数a,b,输出它们的商(a<=10^5000,b<=10^9) 输入输出格式 输入格式: 两行,第一行是被除数,第二行是除数. 输出 ...

  2. Android学习笔记之Bitmap位图虽触摸点移动

    package xiaosi.bitmap; import android.app.Activity; import android.os.Bundle; public class mianActiv ...

  3. amazeui学习笔记三(你来我往1)--常见问题FAQs

    amazeui学习笔记三(你来我往1)--常见问题FAQs 一.总结 1.DOM事件失败:记得加上初始化代码,例如 图片轮播 $('#my-slider').flexslider(); 2.jquer ...

  4. Sql Server 2014完全卸载

    经历过好多次Sql server的安装与卸载,有时发现自己卸载的费时费力,单纯地卸载个软件就要吐血了,那么现在我觉得是时候整理一下了. 1.在运行中输入services.msc,然后找到所有跟Sql ...

  5. ArcGIS在线帮助的使用指南

    一直感觉ArcGIS的在线帮助就是鸡肋,没想到网络常见的所谓的高大上的博文,也不过是对GIS 在线帮助的拷贝,或者简单修改而已.其实ArcGIS的在线帮助包含了以下几个很好用的模块: 备注 ArcGI ...

  6. android studio 一次编译错误:Error:Minimum supported Gradle version is 2.14.1.

    因为需要,今天从git上重新下载工程到另一个目录下,结果运行的时候报了这个错:Error:Minimum supported Gradle version is 2.14.1.  Current ve ...

  7. JS学习笔记 - fgm练习 - 输入法下拉框 三元表达式

    <script> window.onload = function() { var oBtn = document.getElementsByTagName('input')[0]; va ...

  8. 详解Spring Boot配置文件之多环境配置

    一. 多环境配置的好处: 1.不同环境配置可以配置不同的参数~ 2.便于部署,提高效率,减少出错~ 二. properties多环境配置 1. 配置激活选项 spring.profiles.activ ...

  9. TensorFlow 学习(十五)—— tensorflow.python.platform

    tensorflow.python.platform 下的常用工具类和工具函数:tensorflow/tensorflow/python/platform at master · tensorflow ...

  10. JMS服务器ActiveMQ的初体验并持久化消息到MySQL数据库中

    JMS服务器ActiveMQ的初体验并持久化消息到MySQL数据库中 一.JMS的理解JMS(Java Message Service)是jcp组织02-03年定义了jsr914规范(http://j ...