//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,宽字符转窄字符的更多相关文章

  1. 宽字符转窄字符CW2AEX<>(szAreaInfo,CP_UTF8)

    CString szAreaInfo; CW2AEX<>(szAreaInfo,CP_UTF8); 最好能像上面这样转换,否则汉字就会转成乱码.

  2. 宽字符wchar_t和窄字符char——putwchar、wprintf

    宽字符wchar_t 与 窄字符char 先说下窄字符char,这个大部分读者应该很清楚,char类型的变量占一个字节(byte)(也就是8个bit(比特)),能表示256个字符,那char的范围有两 ...

  3. C++ 宽字符(wchar_t)与窄字符(char)的转换

    了解 长度 宽字符wchar_t的长度16位,可以用来显示中文等除英文外的其他文字, 窄字符    char   的长度  8 位,只能处理英文. 哪里可以见到 在VS2010, 2012, 2013 ...

  4. volatile,可变参数,memset,内联函数,宽字符窄字符,国际化,条件编译,预处理命令,define中##和#的区别,文件缓冲,位域

    1.volatile: 要求参数修改每次都从内存中的读取.这种情况要比普通运行的变量需要的时间长. 当设置了成按照C99标准运行之后,使用volatile变量之后的程序运行的时间将比register的 ...

  5. C语言小程序——推箱子(窄字符和宽字符)

    C语言小程序——推箱子(窄字符Version) 推箱子.c #include <stdio.h> #include <conio.h> #include <stdlib. ...

  6. 宽字符wchar_t和窄字符char区别和相互转换

    转自:http://blog.csdn.net/nodeathphoenix/article/details/7416725 1.    首先,说下窄字符char了,大家都很清楚,就是8bit表示的b ...

  7. 关于MultiByteToWideChar与WideCharToMultiByte代码测试(宽字符与多字节字符的转换)以及字符串的转换代码测试

    #pragma once #include <stdio.h> //getchar() #include <tchar.h> #include <stdlib.h> ...

  8. Spark --【宽依赖和窄依赖】

    前言 Spark中RDD的高效与DAG图有着莫大的关系,在DAG调度中需要对计算过程划分stage,暴力的理解就是stage的划分是按照有没有涉及到shuffle来划分的,没涉及的shuffle的都划 ...

  9. Spark 中的宽依赖和窄依赖

    Spark中RDD的高效与DAG图有着莫大的关系,在DAG调度中需要对计算过程划分stage,而划分依据就是RDD之间的依赖关系.针对不同的转换函数,RDD之间的依赖关系分类窄依赖(narrow de ...

随机推荐

  1. 米兰站热卖:奢侈品电商困局已破?-搜狐IT

    米兰站热卖:奢侈品电商困局已破?-搜狐IT 米兰站热卖:奢侈品电商困局已破?

  2. 在VirtualBox虚拟机上采集Fedora15系统

    在VirtualBox虚拟机上采集Fedora15系统 第一部分:创建系统并磁盘分区 1.点击VirtualBox上的新建 2.添加名称,选择类型和版本,点下一步 3.写入内存(不要超过物理内存),点 ...

  3. 04737_C++程序设计_第6章_继承和派生

    例6.1 使用默认内联函数实现单一继承. #include<iostream> using namespace std; class Point { private: int x, y; ...

  4. 愤怒的DZY(二分)

    愤怒的DZY[问题描述]“愤怒的小鸟”如今已经是家喻户晓的游戏了,机智的WJC最近发明了一个类似的新游戏:“愤怒的DZY”.游戏是这样的:玩家有K个DZY,和N个位于不同的整数位置:X1,X2,…,X ...

  5. Android View动画

    Animation TypeEvaluator View的animate方法 ValueAnimator ObjectAnimator AnimatorSet 使用xml来创建动画 animation ...

  6. mac电脑批量解压android apk文件图形化工具--apkDecode

    mac电脑apk文件解压软件,简单的用图形界面将apktools包装了下,使用起来非常简单,可以将apk文件批量解压缩,方便大家查看一些东东,仅供学习目的. 使用步骤如下: 1 下载apkDecode ...

  7. 编写可维护的JS 03

    3.语句和表达式 所有语句都应当使用花括号 if else语句 for 循环 while 循环 do...while try...catch...finally 花括号对齐方式 左括号在第一行语句末尾 ...

  8. JavaScript之arguments.callee

    arguments.callee 在哪一个函数中运行,它就代表哪个函数. 一般用在匿名函数中. 在匿名函数中有时会需要自己调用自己,但是由于是匿名函数,没有名子,无名可调. 这时就可以用argumen ...

  9. Asp.net 获取服务器指定文件夹目录文件,并提供下载

    string dirPath = HttpContext.Current.Server.MapPath("uploads/"); if (Directory.Exists(dirP ...

  10. The type javax.servlet.http.HttpServletRequest cannot be resolved.

    The type javax.servlet.http.HttpServletRequest cannot be resolved. It is indirectly referenced from ...