GBK UTF8 ANSI UNICODE 互转
来源:互联网 发布:linux vi 退出不保存 编辑:程序博客网 时间:2024/05/18 02:55
string GBKToUTF8(const std::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;
}
2、将UTF8转换成GBK
string 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, (LPCTSTR)strUTF8.c_str(), -1, wszGBK, len);
len = WideCharToMultiByte(CP_ACP, 0, wszGBK, -1, NULL, 0, NULL, NULL);
char *szGBK = new char[len + 1];
memset(szGBK, 0, len + 1);
WideCharToMultiByte(CP_ACP,0, wszGBK, -1, szGBK, len, NULL, NULL);
//strUTF8 = szGBK;
std::string strTemp(szGBK);
delete[]szGBK;
delete[]wszGBK;
return strTemp;
}
//UTF-8到GB2312的转换
char* U2G(const char* utf8)
{
int len = MultiByteToWideChar(CP_UTF8, 0, utf8, -1, NULL, 0);
wchar_t* wstr = new wchar_t[len+1];
memset(wstr, 0, len+1);
MultiByteToWideChar(CP_UTF8, 0, utf8, -1, wstr, len);
len = WideCharToMultiByte(CP_ACP, 0, wstr, -1, NULL, 0, NULL, NULL);
char* str = new char[len+1];
memset(str, 0, len+1);
WideCharToMultiByte(CP_ACP, 0, wstr, -1, str, len, NULL, NULL);
if(wstr) delete[] wstr;
return str;
}
//GB2312到UTF-8的转换
char* G2U(const char* gb2312)
{
int len = MultiByteToWideChar(CP_ACP, 0, gb2312, -1, NULL, 0);
wchar_t* wstr = new wchar_t[len+1];
memset(wstr, 0, len+1);
MultiByteToWideChar(CP_ACP, 0, gb2312, -1, wstr, len);
len = WideCharToMultiByte(CP_UTF8, 0, wstr, -1, NULL, 0, NULL, NULL);
char* str = new char[len+1];
memset(str, 0, len+1);
WideCharToMultiByte(CP_UTF8, 0, wstr, -1, str, len, NULL, NULL);
if(wstr) delete[] wstr;
return str;
}
void AtoW(LPSTR psz, LPTSTR &pwsz)
{
ASSERT(psz != NULL);
const int size = MultiByteToWideChar(CP_ACP, 0, psz, -1, NULL, 0);
ASSERT(size > 0);
pwsz = (LPWSTR) malloc(sizeof(WCHAR)*size);
ASSERT(pwsz != NULL);
memset(pwsz, 0, sizeof(WCHAR)*size);
VERIFY(MultiByteToWideChar(CP_ACP, 0, psz, -1, pwsz, size) != 0);
}
void WtoA(LPCWSTR pwsz, LPSTR &m_psz)
{
ASSERT(pwsz != NULL);
const int size = WideCharToMultiByte(CP_ACP, 0, pwsz, -1,
NULL, 0, NULL, NULL);
ASSERT(size > 0);
m_psz = (LPSTR) malloc(sizeof(CHAR)*size);
ASSERT(m_psz != NULL);
WideCharToMultiByte(CP_ACP, 0, pwsz, -1,m_psz, size, NULL, NULL);
}
- GBK UTF8 ANSI UNICODE 互转
- UTF8 转 UNICODE GBK ANSI
- c++ ANSI、UNICODE、UTF8互转
- c++ ANSI、UNICODE、UTF8互转
- c++ ANSI、UNICODE、UTF8互转
- c++ ANSI、UNICODE、UTF8互转
- win32的ANSI、UNICODE、UTF8互转
- Unicode, ANSI, UTF8
- Unicode,Ansi和UTF8
- ANSI Unicode,UTF8 转换
- Ansi Unicode UTF8
- ANSI - Unicode - UTF8 转换
- utf8,unicode ,ansi转换
- ansi-unicode-utf8
- Ansi,UTF8,Unicode编码
- ANSI UTF8 UNICODE
- Ansi,UTF8,Unicode编码
- Ansi,UTF8,Unicode编码
- 表达式解析器添加dynamic访问及静态方法调用
- [jbpm]jBPM(八): 也说jBPM + Tomcat + MySQL
- UITextField
- C语言基础-运算符优先级
- [jbpm]jBPM(九):jBPM的webSale在Tomcat下怎么连上的数据库?
- GBK UTF8 ANSI UNICODE 互转
- SQL Server 2008 通过配置数据库邮件实现发送邮件功能
- [jbpm]jBPM(十):webSale的"页面流"介绍
- ubuntu下网络抓包工具wireshark的使用
- C++学习笔记
- 利用DataGramPacket和DtaGramSocket编写的从服务端获取图片
- [ibpm]jBPM(十一): 从JbpmContextFilter说起
- vs2008的显示界面菜单显示模糊,怎么修改菜单的字体呢?
- [jbpm]jBPM(十二): 流程运行的历史和轨迹与ProcessLog