多字节编码、UTF8编码 、 Unicode编码 互转
2016-11-04 14:52
260 查看
#include<vector>
using namespace std;
#define int32 __int32
vector<char> pu8;
vector<char> pmb;
// 多字节编码转为UTF8编码
bool MBToUTF8(std::vector<char>& pu8, const char* pmb, int32 mLen)
{
// convert an MBCS string to widechar
int32 nLen = MultiByteToWideChar(CP_ACP, 0, pmb, mLen, NULL, 0);
WCHAR* lpszW = NULL;
try
{
lpszW = new WCHAR[nLen];
}
catch(bad_alloc &memExp)
{
return false;
}
int32 nRtn = MultiByteToWideChar(CP_ACP, 0, pmb, mLen, lpszW, nLen);
if(nRtn != nLen)
{
delete[] lpszW;
return false;
}
// convert an widechar string to utf8
int32 utf8Len = WideCharToMultiByte(CP_UTF8, 0, lpszW, nLen, NULL, 0, NULL, NULL);
if (utf8Len <= 0)
{
return false;
}
pu8.resize(utf8Len);
nRtn = WideCharToMultiByte(CP_UTF8, 0, lpszW, nLen, &*pu8.begin(), utf8Len, NULL, NULL);
delete[] lpszW;
if (nRtn != utf8Len)
{
pu8.clear();
return false;
}
return true;
}
// UTF8编码转为多字节编码
bool UTF8ToMB(vector<char>& pmb, const char* pu8, int32 utf8Len)
{
// convert an UTF8 string to widechar
int32 nLen = MultiByteToWideChar(CP_UTF8, 0, pu8, utf8Len, NULL, 0);
WCHAR* lpszW = NULL;
try
{
lpszW = new WCHAR[nLen];
}
catch(bad_alloc &memExp)
{
return false;
}
int32 nRtn = MultiByteToWideChar(CP_UTF8, 0, pu8, utf8Len, lpszW, nLen);
if(nRtn != nLen)
{
delete[] lpszW;
return false;
}
// convert an widechar string to Multibyte
int32 MBLen = WideCharToMultiByte(CP_ACP, 0, lpszW, nLen, NULL, 0, NULL, NULL);
if (MBLen <=0)
{
return false;
}
pmb.resize(MBLen);
nRtn = WideCharToMultiByte(CP_ACP, 0, lpszW, nLen, &*pmb.begin(), MBLen, NULL, NULL);
delete[]
4000
lpszW;
if(nRtn != MBLen)
{
pmb.clear();
return false;
}
return true;
}
// 多字节编码转为Unicode编码
bool MBToUnicode(vector<wchar_t>& pun, const char* pmb, int32 mLen)
{
// convert an MBCS string to widechar
int32 uLen = MultiByteToWideChar(CP_ACP, 0, pmb, mLen, NULL, 0);
if (uLen<=0)
{
return false;
}
pun.resize(uLen);
int32 nRtn = MultiByteToWideChar(CP_ACP, 0, pmb, mLen, &*pun.begin(), uLen);
if (nRtn != uLen)
{
pun.clear();
return false;
}
return true;
}
//Unicode编码转为多字节编码
bool UnicodeToMB(vector<char>& pmb, const wchar_t* pun, int32 uLen)
{
// convert an widechar string to Multibyte
int32 MBLen = WideCharToMultiByte(CP_ACP, 0, pun, uLen, NULL, 0, NULL, NULL);
if (MBLen <=0)
{
return false;
}
pmb.resize(MBLen);
int nRtn = WideCharToMultiByte(CP_ACP, 0, pun, uLen, &*pmb.begin(), MBLen, NULL, NULL);
if(nRtn != MBLen)
{
pmb.clear();
return false;
}
return true;
}
// UTF8编码转为Unicode
bool UTF8ToUnicode(vector<wchar_t>& pun, const char* pu8, int32 utf8Len)
{
// convert an UTF8 string to widechar
int32 nLen = MultiByteToWideChar(CP_UTF8, 0, pu8, utf8Len, NULL, 0);
if (nLen <=0)
{
return false;
}
pun.resize(nLen);
int32 nRtn = MultiByteToWideChar(CP_UTF8, 0, pu8, utf8Len, &*pun.begin(), nLen);
if(nRtn != nLen)
{
pun.clear();
return false;
}
return true;
}
// Unicode编码转为UTF8
bool UnicodeToUTF8(vector<char>& pu8, const wchar_t* pun, int32 uLen)
{
// convert an widechar string to utf8
int32 utf8Len = WideCharToMultiByte(CP_UTF8, 0, pun, uLen, NULL, 0, NULL, NULL);
if (utf8Len<=0)
{
return false;
}
pu8.resize(utf8Len);
int32 nRtn = WideCharToMultiByte(CP_UTF8, 0, pun, uLen, &*pu8.begin(), utf8Len, NULL, NULL);
if (nRtn != utf8Len)
{
pu8.clear();
return false;
}
return true;
}
using namespace std;
#define int32 __int32
vector<char> pu8;
vector<char> pmb;
// 多字节编码转为UTF8编码
bool MBToUTF8(std::vector<char>& pu8, const char* pmb, int32 mLen)
{
// convert an MBCS string to widechar
int32 nLen = MultiByteToWideChar(CP_ACP, 0, pmb, mLen, NULL, 0);
WCHAR* lpszW = NULL;
try
{
lpszW = new WCHAR[nLen];
}
catch(bad_alloc &memExp)
{
return false;
}
int32 nRtn = MultiByteToWideChar(CP_ACP, 0, pmb, mLen, lpszW, nLen);
if(nRtn != nLen)
{
delete[] lpszW;
return false;
}
// convert an widechar string to utf8
int32 utf8Len = WideCharToMultiByte(CP_UTF8, 0, lpszW, nLen, NULL, 0, NULL, NULL);
if (utf8Len <= 0)
{
return false;
}
pu8.resize(utf8Len);
nRtn = WideCharToMultiByte(CP_UTF8, 0, lpszW, nLen, &*pu8.begin(), utf8Len, NULL, NULL);
delete[] lpszW;
if (nRtn != utf8Len)
{
pu8.clear();
return false;
}
return true;
}
// UTF8编码转为多字节编码
bool UTF8ToMB(vector<char>& pmb, const char* pu8, int32 utf8Len)
{
// convert an UTF8 string to widechar
int32 nLen = MultiByteToWideChar(CP_UTF8, 0, pu8, utf8Len, NULL, 0);
WCHAR* lpszW = NULL;
try
{
lpszW = new WCHAR[nLen];
}
catch(bad_alloc &memExp)
{
return false;
}
int32 nRtn = MultiByteToWideChar(CP_UTF8, 0, pu8, utf8Len, lpszW, nLen);
if(nRtn != nLen)
{
delete[] lpszW;
return false;
}
// convert an widechar string to Multibyte
int32 MBLen = WideCharToMultiByte(CP_ACP, 0, lpszW, nLen, NULL, 0, NULL, NULL);
if (MBLen <=0)
{
return false;
}
pmb.resize(MBLen);
nRtn = WideCharToMultiByte(CP_ACP, 0, lpszW, nLen, &*pmb.begin(), MBLen, NULL, NULL);
delete[]
4000
lpszW;
if(nRtn != MBLen)
{
pmb.clear();
return false;
}
return true;
}
// 多字节编码转为Unicode编码
bool MBToUnicode(vector<wchar_t>& pun, const char* pmb, int32 mLen)
{
// convert an MBCS string to widechar
int32 uLen = MultiByteToWideChar(CP_ACP, 0, pmb, mLen, NULL, 0);
if (uLen<=0)
{
return false;
}
pun.resize(uLen);
int32 nRtn = MultiByteToWideChar(CP_ACP, 0, pmb, mLen, &*pun.begin(), uLen);
if (nRtn != uLen)
{
pun.clear();
return false;
}
return true;
}
//Unicode编码转为多字节编码
bool UnicodeToMB(vector<char>& pmb, const wchar_t* pun, int32 uLen)
{
// convert an widechar string to Multibyte
int32 MBLen = WideCharToMultiByte(CP_ACP, 0, pun, uLen, NULL, 0, NULL, NULL);
if (MBLen <=0)
{
return false;
}
pmb.resize(MBLen);
int nRtn = WideCharToMultiByte(CP_ACP, 0, pun, uLen, &*pmb.begin(), MBLen, NULL, NULL);
if(nRtn != MBLen)
{
pmb.clear();
return false;
}
return true;
}
// UTF8编码转为Unicode
bool UTF8ToUnicode(vector<wchar_t>& pun, const char* pu8, int32 utf8Len)
{
// convert an UTF8 string to widechar
int32 nLen = MultiByteToWideChar(CP_UTF8, 0, pu8, utf8Len, NULL, 0);
if (nLen <=0)
{
return false;
}
pun.resize(nLen);
int32 nRtn = MultiByteToWideChar(CP_UTF8, 0, pu8, utf8Len, &*pun.begin(), nLen);
if(nRtn != nLen)
{
pun.clear();
return false;
}
return true;
}
// Unicode编码转为UTF8
bool UnicodeToUTF8(vector<char>& pu8, const wchar_t* pun, int32 uLen)
{
// convert an widechar string to utf8
int32 utf8Len = WideCharToMultiByte(CP_UTF8, 0, pun, uLen, NULL, 0, NULL, NULL);
if (utf8Len<=0)
{
return false;
}
pu8.resize(utf8Len);
int32 nRtn = WideCharToMultiByte(CP_UTF8, 0, pun, uLen, &*pu8.begin(), utf8Len, NULL, NULL);
if (nRtn != utf8Len)
{
pu8.clear();
return false;
}
return true;
}
相关文章推荐
- python中文utf8编码后是占3个字符,unicode汉字为2字节
- python中文utf8编码后是占3个字符,unicode汉字为2字节
- 多字节(一般指GBK) utf8 Unicode 编码互转
- VS2010与VS2013中的多字节编码与Unicode编码问题
- Python 有关unicode,gbk,cp2312,utf8的解码和编码
- MFC 多字节和UNICODE的相互转换(解决中文utf8-ASCNII乱码问题)
- Java实现的utf8,gbk,unicode编码相互转换的代码
- python编码处理:unicode字节串转成中文 各种字符串举例说明
- 将UTF8编码转换成Unicode(UCS-2)编码
- 编码,UTF8,UNICODE
- 多字节编码转为UTF8编码 & UTF8编码转为多字节
- 所谓编码--泛谈ASCII、Unicode、UTF8、UTF16、UCS-2等编码格式
- ASCII,Unicode,UTF8编码的认识
- Unicode编码,以及实现方式UTF8的实现方式
- Unicode utf8等编码类型的原理
- 常用字库编码的转换(Unicode,Utf8,Big5,Gb18030)
- 查询任意汉字的Unicode编码,UTF8编码,GB2312编码,GBK编码
- 【转】 常用字库编码的转换(Unicode,Utf8,Big5,Gb18030)
- Unicode,ISO-10646,UTF8编码技术
- unicode编码与utf8互转