// 多字节编码转为UTF8编码
bool MBToUTF8(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[] 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;
}
分享到:
相关推荐
易语言字符编码转换源码,字符编码转换
"批量字符编码转换工具"是绿色软件,单文件免安装,请放心试用。 批量字符编码转换工具可以在gbk,gb23123,unicode,utf8,ansi之间进行汉字编码转换。 批量字符编码转换工具的特色: 1 支持格式众多的字符串编码转换器 ...
编码转换工具,支持40种编码格式转换,支持目录下所有文件编码转换和筛选不同后缀的文件。
JS实现URL编码转换中文
汉字编码转换工具,实现了汉字与 utf-8 gb2312 unicode 互转,开发者多百多度 qq:547170882
CTF Converter 编码转换
易语言iconv.fne支持库中文名为易语言编码转换支持库,本易语言支持库在转换编码时使用 GNU libiconv 1.9.1版,支持现有绝大多数编码和字符集。易语言iconv.fne支持库为一般支持库,需要易系统3.7版本的支持,需要...
TXT批量编码转换器TXT批量编码转换器TXT批量编码转换器TXT批量编码转换器
C++各种编码转换 Unicode UTF8
文字编码转换工具,具有Native,unicode,UTF8等各种编码格式互相转换的功能。
ASCII码及UCS2编码转换器,包含中文编解码,C#编写,用过都说好,ASCII码及UCS2编码转换器,包含中文编解码,C#编写,用过都说好,
编码转换可转utf-8等..............
资源介绍:。易语言编码转换将任何编码转换到GB2312模块源码。资源作者:。易语言源码。资源下载:。
编码转换工具,用于给各种字符集的文件自动,批量转换编码
一个好用的文本文件编码转换器,可以实现文本文件的编码转换,支持常用编码转换,例如Unicode,utf-8,ANSI之间相互转化
ConvertZ 强大的编码转换工具,方便各种类型之间的转换
游戏用各种字符编码转换..........................
java编码转换工具
字符编码过滤器 j2ee字符字符编码 字符编码过滤器 字符编码转换 post字符转换
C语言字符编码转换UNICODE、GBK、UTF-8互相转换