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 ...
随机推荐
- CLI:使用Go开发命令行应用
原文地址 CLI或者"command line interface"是用户在命令行下交互的程序.由于通过将程序编译到一个静态文件中来减少依赖,一次Go特别适合开发CLI程序.如 ...
- Leetcode: LFU Cache && Summary of various Sets: HashSet, TreeSet, LinkedHashSet
Design and implement a data structure for Least Frequently Used (LFU) cache. It should support the f ...
- ferret32位安装
首先在网上找到解决方案: 1.添加对32位的支持 dpkg --add-architecture i386 2.更新 apt-get clean && apt-get update & ...
- fopen,fread和fwrite
在最近的编程练习和写东西的过程中,常常用到了fopen和fread两个函数来读取本地文件.之前使用这两个函数时,一直没有出现过什么问题.也是因为没有出现问题,对这两个函数的用法的一些细节没有很了解,所 ...
- paper 115:常见的概率分布(matlab作图)
一.常见的概率分布 表1.1 概率分布分类表 连续随机变量分布 连续统计量分布 离散随机变量分布 分布 分布 二项分布 连续均匀分布 非中心 分布 离散均匀分布 (Gamma)分布 分布 几何分布 指 ...
- 802.1X基础
这是一个认证规范.使用EAPOL协议在客户端与认证端交互. EAPOL协议:Extensible Authentication Protocol over LAN. 假设三个实体: 客户端:PC 认证 ...
- How to install flashplugin on ubuntu
sudo apt-get install flashplugin-installer
- MVC3 数据验证用法之密码验证设计思路
描述:MVC数据验证使用小结 内容:display,Required,stringLength,Remote,compare,RegularExpression 本人最近在公司用mvc做了一个修改密码 ...
- SVN在eclipse的整合应用
目前很多的Java.Flex.Android开发人员是用eclipse作为开发工具的,本文主要介绍SVN在eclipse平台中的整合应用. 我的eclipse版本是Version: 3.4.2.本身没 ...
- Oracle 正则表达式使用示例
正则表达式的基本例子 在使用这个新功能之前,您需要了解一些元字符的含义.句号 (.) 匹配一个正规表达式中的任意字符(除了换行符).例如,正规表达式 a.b 匹配的字符串中首先包含字母 a,接着是其它 ...