多字节编码转为UTF8编码 & UTF8编码转为多字节
2012-07-20 14:15
141 查看
开发环境:VS2008
#include <iostream>
#include <windows.h>
#include <string>
void ConvertGBKToUtf8(std::string& amp, std::string strGBK)
{
int len=MultiByteToWideChar(CP_ACP, 0, (LPCSTR)strGBK.c_str(), -1, NULL,0);
unsigned short * wszUtf8 = new unsigned short[len+1];
memset(wszUtf8, 0, len * 2 + 2);
MultiByteToWideChar(CP_ACP, 0, (LPCSTR)strGBK.c_str(), -1, (LPWSTR)wszUtf8, len);
len = WideCharToMultiByte(CP_UTF8, 0, (LPCWSTR)wszUtf8, -1, NULL, 0, NULL, NULL);
char *szUtf8=new char[len + 1];
memset(szUtf8, 0, len + 1);
WideCharToMultiByte (CP_UTF8, 0, (LPCWSTR)wszUtf8, -1, szUtf8, len, NULL,NULL);
//strGBK = szUtf8;
amp=szUtf8;
delete[] szUtf8;
delete[] wszUtf8;
}
void ConvertUtf8ToGBK(std::string&, std::string strUtf8)
{
int len=MultiByteToWideChar(CP_UTF8, 0, (LPCSTR)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, (LPCWSTR)wszGBK, -1, NULL, 0, NULL, NULL);
char *szGBK=new char[len + 1];
memset(szGBK, 0, len + 1);
WideCharToMultiByte (CP_ACP, 0, (LPCWSTR)wszGBK, -1, szGBK, len, NULL,NULL);
//strUtf8 = szGBK;
amp=szGBK;
delete[] szGBK;
delete[] wszGBK;
}
int main(int argc, char *argv)
{
std::string amp;
std::string strGBK = "china 中国人民!!";
ConvertGBKToUtf8(amp, strGBK);
std::wcout << "/////////////////////////////////// " << std::endl;
std::string temp;
ConvertUtf8ToGBK(temp, amp);
std::cout << "Utf8ToGBK : " << temp << std::endl;
system("PAUSE");
}
#include <iostream>
#include <windows.h>
#include <string>
void ConvertGBKToUtf8(std::string& amp, std::string strGBK)
{
int len=MultiByteToWideChar(CP_ACP, 0, (LPCSTR)strGBK.c_str(), -1, NULL,0);
unsigned short * wszUtf8 = new unsigned short[len+1];
memset(wszUtf8, 0, len * 2 + 2);
MultiByteToWideChar(CP_ACP, 0, (LPCSTR)strGBK.c_str(), -1, (LPWSTR)wszUtf8, len);
len = WideCharToMultiByte(CP_UTF8, 0, (LPCWSTR)wszUtf8, -1, NULL, 0, NULL, NULL);
char *szUtf8=new char[len + 1];
memset(szUtf8, 0, len + 1);
WideCharToMultiByte (CP_UTF8, 0, (LPCWSTR)wszUtf8, -1, szUtf8, len, NULL,NULL);
//strGBK = szUtf8;
amp=szUtf8;
delete[] szUtf8;
delete[] wszUtf8;
}
void ConvertUtf8ToGBK(std::string&, std::string strUtf8)
{
int len=MultiByteToWideChar(CP_UTF8, 0, (LPCSTR)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, (LPCWSTR)wszGBK, -1, NULL, 0, NULL, NULL);
char *szGBK=new char[len + 1];
memset(szGBK, 0, len + 1);
WideCharToMultiByte (CP_ACP, 0, (LPCWSTR)wszGBK, -1, szGBK, len, NULL,NULL);
//strUtf8 = szGBK;
amp=szGBK;
delete[] szGBK;
delete[] wszGBK;
}
int main(int argc, char *argv)
{
std::string amp;
std::string strGBK = "china 中国人民!!";
ConvertGBKToUtf8(amp, strGBK);
std::wcout << "/////////////////////////////////// " << std::endl;
std::string temp;
ConvertUtf8ToGBK(temp, amp);
std::cout << "Utf8ToGBK : " << temp << std::endl;
system("PAUSE");
}
相关文章推荐
- 在 Perl看来, 字符串只有两种形式. 一种是octets, 即8位序列, 也就是我们通常说的字节数组. 另一种utf8编码的字符串, perl管它叫string. 也就是说: Perl只熟悉两种编
- Atitit.字节数组转字符串 base64 base16 Quoted-printable 编码原理设计 attilax 总结
- 字符集编码插入Mysql数据库是,字符utf8编码超过3位时的错误以及解决,Incorrect string value: '\xF0\xA1\xBF\xA8\
- 多字节(一般指GBK) utf8 Unicode 编码互转
- Encode/Decode&ANSI<->UTF8两个编码工具 下载
- 在 Perl看来, 字符串只有两种形式. 一种是octets, 即8位序列, 也就是我们通常说的字节数组. 另一种utf8编码的字符串, perl管它叫string. 也就是说: Perl只熟悉两种编
- 黑马程序员_<<IO扩展对象和编码(序列化,管道流,字节(符)数组),随机访问文件>>
- 字节编码类型判断<转>
- IO_缓冲流_转换流_字节转为字符_乱码分析_编码与解码JAVA152-154
- 汉字UTF8编码占用几个字节
- 批量把文件编码由GBK转为UTF8
- Android之NDK编码转(UTF8->GBK)
- ASCII+Unicode+UTF8(字符编码方式->字节) Base64(字节编码方式->64字符)
- 多字节编码、UTF8编码 、 Unicode编码 互转
- php字符编码转换之gb2312转为utf8
- js将图片转为base64编码 && js将base64编码图片转为Blob格式
- 多字节编码字符串与UTF8字符编码的转换
- c++ UTF8编码转为多字节编码
- utf8编码-汉字几字节
- 自动探测gbk编码文件并转为utf8