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 ...
随机推荐
- GMap.NET二次开发库
GMap.NET的出现,解决了传统的依赖于地图引擎的代价高昂的解决方案,而且使用传统地图引擎,无法迁移,就连我们的地图放大缩小这样常规的方法,都是紧紧绑定在这些令人恶心的地图引擎开发包提供的SDK接口 ...
- Design and Analysis of Algorithms_Fundamentals of the Analysis of Algorithm Efficiency
I collect and make up this pseudocode from the book: <<Introduction to the Design and Analysis ...
- sql server 数据库 日期格式转换
日期时间转字符串:Select CONVERT(varchar(100), GETDATE(), 0): 05 16 2006 10:57AM Select CONVERT(varchar(100), ...
- 非常简单的数据,支持excel表格下载功能
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"% ...
- github Android-Universal-Image-Loader
目存在于Github上面https://github.com/nostra13/Android-Universal-Image-Loader,我们可以先看看这个开源库存在哪些特征 多线程下载图片,图片 ...
- DEV全选多选小技巧
var v1 = bindingSourceBase.DataSource as DataTable; foreach (DataRowView v in v1.DefaultView) { v[&q ...
- Html5shiv
说明编辑 越来越多的站点开始使用 HTML5 标签.但情况是还有很多人在使用IE6,IE7,IE8.为了让所有网站浏览者都能正常的访问网站, 2解决方案编辑 有下面两个: 为网站创建多套模板,通过程序 ...
- centOs安装jdk1.7
1,下载jdk-7u75-linux-x64.tar.gz,地址http://www.oracle.com/technetwork/java/javase/downloads/jdk7-downloa ...
- Remove-Azureaccount (Can't get Azure credentials to stick in Powershel)
https://social.technet.microsoft.com/forums/azure/en-US/260df055-7c4e-4ce2-8f8d-190ad20a4b76/cant-ge ...
- js 小数相加
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat=&qu ...