C++ 字符编码转换类
记录一下C++ 编码转换的函数:
#pragma once
#include "afx.h" #define DEFAULT_CODE 0
#define CHINESE_SIMPLIFIED 1
#define CHINESE_TRADITIONAL 2 class CChineseConvertor:
//public CObject
{
public:
CChineseConvertor(void);
~CChineseConvertor(void);
LPSTR Big52GBKSimplified(char * szText);
LPSTR Big52GBKTraditional(char * szText);
LPSTR GBK2Big5(char * szText);
LPSTR GBKSimplified2GBKTraditional(char * szSimplified);
LPSTR GBKTraditional2GBKSimplified(char * szTraditional);
LPWSTR UTF82UNICODE(char* utf8str);
LPSTR UNICODE2UTF8(LPCWSTR strText); char *m_pszUnknown;
// 转换到Unicode
LPWSTR ToUnicode(char * szSource, int nEncoding);
LPSTR ToMultiByte(LPCWSTR szSource, int nEncoding);
};
#include "stdafx.h"
#include "Coding.h" CChineseConvertor::CChineseConvertor(void)
{
m_pszUnknown = new char[];
m_pszUnknown[]=' ';
m_pszUnknown[]=;
} CChineseConvertor::~CChineseConvertor(void)
{
delete[] m_pszUnknown;
m_pszUnknown = NULL;
} //big5 to GBK_简体
LPSTR CChineseConvertor::Big52GBKSimplified(char * szText)
{
int nLength;
wchar_t *pBuffer;
LPSTR pResult;
int nResultLength; nLength=MultiByteToWideChar(,,szText,strlen(szText),NULL,);
pBuffer=new wchar_t[nLength+];
MultiByteToWideChar(,,(LPCSTR)szText,strlen(szText),(LPWSTR)pBuffer,nLength);
pBuffer[nLength]=; nResultLength=WideCharToMultiByte(,,pBuffer,nLength,NULL,,m_pszUnknown,FALSE);
pResult=new char[nResultLength+];
WideCharToMultiByte(,,(LPWSTR)pBuffer,nLength,(LPSTR)pResult,nResultLength," ",FALSE);
pResult[nResultLength]=; return GBKTraditional2GBKSimplified(pResult); } //big5 to GBK_繁体
LPSTR CChineseConvertor::Big52GBKTraditional(char * szText)
{
int nLength;
wchar_t *pBuffer;
LPSTR pResult;
int nResultLength; nLength=MultiByteToWideChar(,,szText,strlen(szText),NULL,);
pBuffer=new wchar_t[nLength+];
MultiByteToWideChar(,,(LPCSTR)szText,strlen(szText),(LPWSTR)pBuffer,nLength);
pBuffer[nLength]=; nResultLength=WideCharToMultiByte(,,pBuffer,nLength,NULL,,m_pszUnknown,FALSE);
pResult=new char[nResultLength+];
WideCharToMultiByte(,,(LPWSTR)pBuffer,nLength,(LPSTR)pResult,nResultLength," ",FALSE);
pResult[nResultLength]=; return pResult;
} //GBK_简体 to GBK_繁体
LPSTR CChineseConvertor::GBKTraditional2GBKSimplified(char * szTraditional)
{
LCID dwLocale;
WORD wLangID;
wLangID=MAKELANGID(LANG_CHINESE,SUBLANG_CHINESE_SIMPLIFIED);
dwLocale=MAKELCID(wLangID,SORT_CHINESE_PRC); int nLength;
char *pBuffer;
nLength=LCMapStringA(dwLocale,LCMAP_SIMPLIFIED_CHINESE,(LPCSTR)szTraditional,strlen(szTraditional),NULL,);
pBuffer=new char[nLength+];
pBuffer[nLength]=;
LCMapStringA(dwLocale,LCMAP_SIMPLIFIED_CHINESE,(LPCSTR)szTraditional,strlen(szTraditional),pBuffer,nLength);
return pBuffer;
} //GBK_简体 to big5
LPSTR CChineseConvertor::GBK2Big5(char * szText)
{
LPSTR szGBKTraditional;
int nLength;
wchar_t *pBuffer;
LPSTR pResult;
int nResultLength; szGBKTraditional=GBKSimplified2GBKTraditional(szText);
nLength=MultiByteToWideChar(,,szGBKTraditional,strlen(szGBKTraditional),NULL,);
pBuffer=new wchar_t[nLength+];
MultiByteToWideChar(,,(LPCSTR)szGBKTraditional,strlen(szGBKTraditional),(LPWSTR)pBuffer,nLength);
pBuffer[nLength]=; nResultLength=WideCharToMultiByte(,,pBuffer,nLength,NULL,,m_pszUnknown,FALSE);
pResult=new char[nResultLength+];
WideCharToMultiByte(,,(LPWSTR)pBuffer,nLength,(LPSTR)pResult,nResultLength," ",FALSE);
pResult[nResultLength]=; return pResult;
} //将GBK的简体转换到GBK繁体
LPSTR CChineseConvertor::GBKSimplified2GBKTraditional(char * szSimplified)
{
LCID dwLocale;
WORD wLangID;
wLangID=MAKELANGID(LANG_CHINESE,SUBLANG_CHINESE_SIMPLIFIED);
dwLocale=MAKELCID(wLangID,SORT_CHINESE_PRC); int nLength;
char *pBuffer;
nLength=LCMapStringA(dwLocale,LCMAP_TRADITIONAL_CHINESE,(LPCSTR)szSimplified,strlen(szSimplified),NULL,);
pBuffer=new char[nLength+];
pBuffer[nLength]=;
LCMapStringA(dwLocale,LCMAP_TRADITIONAL_CHINESE,(LPCSTR)szSimplified,strlen(szSimplified),pBuffer,nLength);
return pBuffer;
} // 转换到Unicode
LPWSTR CChineseConvertor::ToUnicode(char * szSource, int nEncoding)
{
int nLength;
wchar_t *pBuffer;
int nLanguage; if(nEncoding==CHINESE_SIMPLIFIED)
nLanguage=;
else
if(nEncoding==CHINESE_TRADITIONAL)
nLanguage=;
else
nLanguage= CP_ACP; nLength=MultiByteToWideChar(nLanguage,,szSource,strlen(szSource),NULL,);
pBuffer=new wchar_t[nLength+];
MultiByteToWideChar(nLanguage,,(LPCSTR)szSource,strlen(szSource),(LPWSTR)pBuffer,nLength);
pBuffer[nLength]=; return pBuffer;
} //转换到多字节
LPSTR CChineseConvertor::ToMultiByte(LPCWSTR szSource, int nEncoding)
{
int nLength;
char *pBuffer;
int nLanguage; if(nEncoding==CHINESE_SIMPLIFIED)
nLanguage=;
else
if(nEncoding==CHINESE_TRADITIONAL)
nLanguage=;
else
nLanguage= CP_ACP; nLength=WideCharToMultiByte(nLanguage,,szSource,wcslen(szSource),NULL,,m_pszUnknown,FALSE); pBuffer=new char[nLength+];
WideCharToMultiByte(nLanguage,,szSource,wcslen(szSource),pBuffer,nLength,m_pszUnknown,FALSE);
pBuffer[nLength]=; return pBuffer; } //UTF8转换到UNICODE
LPWSTR CChineseConvertor::UTF82UNICODE(char* utf8str)
{
int nLength;
wchar_t *pBuffer; nLength=MultiByteToWideChar(CP_UTF8,,utf8str,strlen(utf8str),NULL,);
pBuffer=new wchar_t[nLength+];
MultiByteToWideChar(CP_UTF8,,(LPCSTR)utf8str,strlen(utf8str),(LPWSTR)pBuffer,nLength);
pBuffer[nLength]=; return pBuffer;
} //UNICODE转换到UTF8
LPSTR CChineseConvertor::UNICODE2UTF8(LPCWSTR strText)
{
int len;
len = WideCharToMultiByte(CP_UTF8, , (LPCWSTR)strText, -, NULL, , NULL, NULL);
char *szUtf8=new char[*(len + )];
memset(szUtf8, , len * + ); //UTF8最多的字节数最多是一个UINICODE字符所占字节数的两倍
WideCharToMultiByte (CP_UTF8, , (LPCWSTR)strText, -, szUtf8, len, NULL,NULL);
return szUtf8; }
C++ 字符编码转换类的更多相关文章
- 编码问题 php字符编码转换类
各种平台和软件打开显示的编码问题,需要使用不同的编码,根据我们不同的需求. php 字符编码转换类,支持ANSI.Unicode.Unicode big endian.UTF-8.UTF-8+Bom ...
- iconv字符编码转换
转自 http://blog.csdn.net/langresser_king/article/details/7459367 iconv(http://www.gnu.org/software/li ...
- php字符编码转换之gb2312转为utf8(转)
在php中字符编码转换我们一般会用到iconv与mb_convert_encoding进行操作,但是mb_convert_encoding在转换性能上比iconv要差很多哦.string iconv ...
- Char Tools,方便的字符编码转换小工具
工作关系,常有字符编码转换方面的需要,写了这个小工具 Char Tools是一款方便的字符编码转换小工具,基于.Net Framework 2.0 Winform开发 主要功能 URL编码:URLEn ...
- php 字符编码转换函数 iconv mb_convert_encoding比较
在使用PHP处理字符串时,我们经常会碰到字符编码转换的问题,你碰到过iconv转换失败吗? 发现问题时,网上搜了搜,才发现iconv原来有bug ,碰到一些生僻字就会无法转换,当然了配置第二个参数时, ...
- Python—字符编码转换、函数基本操作
字符编码转换 函数 #声明文件编码,格式如下: #-*- coding:utf-8 -*- 注意此处只是声明了文件编码格式,python的默认编码还是unicode 字符编码转换: import sy ...
- day4学python 字符编码转换+元组概念
字符编码转换+元组概念 字符编码转换 #coding:gbk //此处必声明 文件编码(看右下角编码格式) #用来得到python默认编码 import sys print(sys.getdefaul ...
- erlang中字符编码转换(转)
转自:http://www.thinksaas.cn/group/topic/244329/ 功能说明: erlang中对各种语言的编码支持不足,此代码是使用erlang驱动了著名的iconv编码库来 ...
- Qt代码区字符编码转换
在做通讯练习的时候,发现发送给小助手字符乱码,图片如下 本人Qt Creator是UTF-8,需要改成gbk,代码如下 #include<QTextCodec> // 提供字符编码转换 Q ...
随机推荐
- leetcode 397
题目描述: Given a positive integer n and you can do operations as follow: If n is even, replace n with n ...
- Windows Server 2008 任务计划无法自动运行的解决办法
问题:编写的bat脚本,直接执行,成功:但是在任务管理器中配置该任务,运行不成功,结果显示为:0x1,系统环境为 Windows Server 2008. 分析:bat任务没有调用执行. 解决方案: ...
- noi 1.5 45:金币
描述 国王将金币作为工资,发放给忠诚的骑士.第一天,骑士收到一枚金币:之后两天(第二天和第三天)里,每天收到两枚金币:之后三天(第四.五.六天)里,每天收到三枚金币:之后四天(第七.八.九.十天)里, ...
- poj 3262 Protecting the Flowers
http://poj.org/problem?id=3262 Protecting the Flowers Time Limit: 2000MS Memory Limit: 65536K Tota ...
- union和union all有什么不同?
union和union all有什么不同? 相同点:用来获取两个或者两个以上结果集的并集 不同点: union会自动去重,排序 union all没有去重,排序
- HDU 5458 Stability(双连通分量+LCA+并查集+树状数组)(2015 ACM/ICPC Asia Regional Shenyang Online)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5458 Problem Description Given an undirected connecte ...
- 论文阅读(Chenyi Chen——【ACCV2016】R-CNN for Small Object Detection)
Chenyi Chen--[ACCV2016]R-CNN for Small Object Detection 目录 作者和相关链接 方法概括 创新点和贡献 方法细节 实验结果 总结与收获点 参考文献 ...
- Node.js Express 框架 POST方法
POST 方法 以下实例演示了在表单中通过 POST 方法提交两个参数,我们可以使用 server.js 文件内的 process_post 路由器来处理输入: index.htm 文件代码修改如下: ...
- Android Native 代码NDK开发学习笔记
引用:http://www.kunli.info/2011/08/21/android-native-code-study-note/ JNI,全称Java Native Interface,是用于让 ...
- Objective-C基础3
1.OC内存管理 1)范围:管理任何继承NSObject的对象,对其他的基本数据类型无效(堆区),否则会造成内存泄露 2)原理:任何对象都可能有用一个或多个所有者,只要一个对象至少还拥有一个所有者,它 ...