GBK转utf-8,宽字符转窄字符
//GBK转UTF8
string CAppString::GBKToUTF8(const string & strGBK)
{
string strOutUTF8 = "";
WCHAR * str1;
int n = MultiByteToWideChar(CP_ACP, 0, strGBK.c_str(), -1, NULL, 0);
str1 = new WCHAR[n];
MultiByteToWideChar(CP_ACP, 0, strGBK.c_str(), -1, str1, n);
n = WideCharToMultiByte(CP_UTF8, 0, str1, -1, NULL, 0, NULL, NULL);
char * str2 = new char[n];
WideCharToMultiByte(CP_UTF8, 0, str1, -1, str2, n, NULL, NULL);
strOutUTF8 = str2;
delete []str1;
str1 = NULL;
delete []str2;
str2 = NULL;
return strOutUTF8;
} //UTF8转GBK
string CAppString::UTF8ToGBK(const std::string & strUTF8)
{
int len = MultiByteToWideChar(CP_UTF8, 0, strUTF8.c_str(), -1, NULL, 0);
unsigned short * wszGBK = new unsigned short[len + 1];
memset(wszGBK, 0, len * 2 + 2);
MultiByteToWideChar(CP_UTF8, 0, (LPCSTR)strUTF8.c_str(), -1, (LPWSTR)wszGBK, len); len = WideCharToMultiByte(CP_ACP, 0, (LPWSTR)wszGBK, -1, NULL, 0, NULL, NULL);
char *szGBK = new char[len + 1];
memset(szGBK, 0, len + 1);
WideCharToMultiByte(CP_ACP,0, (LPWSTR)wszGBK, -1, szGBK, len, NULL, NULL);
//strUTF8 = szGBK;
std::string strTemp(szGBK);
delete[]szGBK;
delete[]wszGBK;
return strTemp;
} //宽字符转窄
string CAppString::Unicode2ACSII(const wstring & strSource)
{
string strDest("");
if (strSource.empty())
{
return strDest;
} int nlen = ::WideCharToMultiByte(CP_ACP, 0, strSource.c_str(), -1, NULL, 0, NULL, NULL);
char * szDest = new char[nlen + 1];
::WideCharToMultiByte(CP_ACP, 0, strSource.c_str(), -1, szDest, nlen, NULL, NULL);
strDest = szDest;
delete szDest;
szDest = NULL; return strDest;
} //窄字符转宽字符
wstring CAppString::ASCII2Unicode(const char* strSrc)
{
wchar_t* pElementText = NULL;
int nTextLen = 0;
// multi char to wide char
nTextLen = MultiByteToWideChar( CP_ACP, 0, strSrc, -1, NULL, 0 );
pElementText = new wchar_t[nTextLen + 1];
memset(( void* )pElementText, 0, sizeof( wchar_t ) * ( nTextLen + 1 ) );
::MultiByteToWideChar(CP_ACP, 0, strSrc, -1, pElementText, nTextLen);
wstring sText;
sText = pElementText;
delete[] pElementText;
return sText;
}
GBK转utf-8,宽字符转窄字符的更多相关文章
- 宽字符转窄字符CW2AEX<>(szAreaInfo,CP_UTF8)
CString szAreaInfo; CW2AEX<>(szAreaInfo,CP_UTF8); 最好能像上面这样转换,否则汉字就会转成乱码.
- 宽字符wchar_t和窄字符char——putwchar、wprintf
宽字符wchar_t 与 窄字符char 先说下窄字符char,这个大部分读者应该很清楚,char类型的变量占一个字节(byte)(也就是8个bit(比特)),能表示256个字符,那char的范围有两 ...
- C++ 宽字符(wchar_t)与窄字符(char)的转换
了解 长度 宽字符wchar_t的长度16位,可以用来显示中文等除英文外的其他文字, 窄字符 char 的长度 8 位,只能处理英文. 哪里可以见到 在VS2010, 2012, 2013 ...
- volatile,可变参数,memset,内联函数,宽字符窄字符,国际化,条件编译,预处理命令,define中##和#的区别,文件缓冲,位域
1.volatile: 要求参数修改每次都从内存中的读取.这种情况要比普通运行的变量需要的时间长. 当设置了成按照C99标准运行之后,使用volatile变量之后的程序运行的时间将比register的 ...
- C语言小程序——推箱子(窄字符和宽字符)
C语言小程序——推箱子(窄字符Version) 推箱子.c #include <stdio.h> #include <conio.h> #include <stdlib. ...
- 宽字符wchar_t和窄字符char区别和相互转换
转自:http://blog.csdn.net/nodeathphoenix/article/details/7416725 1. 首先,说下窄字符char了,大家都很清楚,就是8bit表示的b ...
- 关于MultiByteToWideChar与WideCharToMultiByte代码测试(宽字符与多字节字符的转换)以及字符串的转换代码测试
#pragma once #include <stdio.h> //getchar() #include <tchar.h> #include <stdlib.h> ...
- Spark --【宽依赖和窄依赖】
前言 Spark中RDD的高效与DAG图有着莫大的关系,在DAG调度中需要对计算过程划分stage,暴力的理解就是stage的划分是按照有没有涉及到shuffle来划分的,没涉及的shuffle的都划 ...
- Spark 中的宽依赖和窄依赖
Spark中RDD的高效与DAG图有着莫大的关系,在DAG调度中需要对计算过程划分stage,而划分依据就是RDD之间的依赖关系.针对不同的转换函数,RDD之间的依赖关系分类窄依赖(narrow de ...
随机推荐
- flex4 日期类型字符串转日期类型(string转Date)(转)
mysql数据库中存储的日期类型通过PHP返回到flex端为字符串类型,这样在flex中进行处理时就必须要将字符串转化为Date类型.如果仅仅是 "年/月/日" 的组合,而没有涉及 ...
- 数组Magic Index
Question A magic index in an array A[1...n-1] is defined to be an index such that A[i] = i. Given a ...
- CC++初学者编程教程(3) 安装VS2010 boost标准库开发环境
1. BOOST编译过程非常复杂,目前为了学习BOOST,首先搭建基于VS2010的BOOST开发环境. Boost库 8 9. 10. 11 12 13 14 15. 16. 17. 18 ...
- 排序-java
今天座右铭----每天的学习会让我们不断地进步! 往往面试中都会让我们用一种排序方法做一道排序题,下面我就罗列出快速排序.冒泡排序.插入排序.选择排序的java代码! 1.快速排序 public cl ...
- Crisis of HDU(母函数)
Crisis of HDU Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tot ...
- bootstrap 响应式布局 居中问题
大家能够通过table来设置居中: display: table; width: auto; margin-left: auto; margin-right: auto;
- Spring MVC 完整示例
在本例中,我们将使用Spring MVC框架构建一个入门级web应用程序.Spring MVC 是Spring框架最重要的的模块之一.它以强大的Spring IoC容器为基础,并充分利用容器的特性来简 ...
- ORA-03113: 通信通道的文件结尾 进程 ID: 764 会话 ID: 125 序列号: 5
昨天因为导入很久数据,最后一看是因为数据文件不够,后来就关机了.现在,开启数据库,总是报“ORA-03113: 通信通道的文件结尾” SQL> conn /as sysdba; 已连接到空闲例程 ...
- J - 搞笑版费马大定理
J - 搞笑版费马大定理 Time Limit:1000MS Memory Limit:131072KB 64bit IO Format:%lld & %llu Submit ...
- 【转】Eclipse自动补全的设置方法
转自:http://blog.csdn.net/xiadasong007/archive/2009/11/11/4799715.aspx 打开 Eclipse -> Window -> P ...