我有一个函数可以将各种编码的字符串转换为Windows内部使用的Unicode-16。为此,我使用了MultiByteToWideChar API。但我刚刚发现了以下内容:
//See how much data do we need?
//UNIT nCodePage = 1201; // just as an example
UINT nchLen = ::MultiByteToWideChar(nCodePage, 0,
pByteArrayToConvert, ncbSzByteArrayToConvert, NULL, 0);
if(!nchLen)
{
//Failed
}
对于以下带有错误代码ERROR_INVALID_PARAMETER
(87)的Unicode代码页失败:
> 1200 utf-16 Unicode UTF-16, little endian byte order
> 1201 unicodeFFFE Unicode UTF-16, big endian byte order
> 12000 utf-32 Unicode UTF-32, little endian byte order
> 12001 utf-32BE Unicode UTF-32, big endian byte order
知道为什么以及如何进行这些转换?
答案 0 :(得分:4)
Windows根本不支持UTF-32,您必须手动实现。
MultiByteToWideChar()
不支持来自 UTF-16或UTF-32的转换。另一方面,对于代码页1200和1201,您的输入数据已经以UTF-16格式显示。 MultiByteToWideChar()
输出UTF-16LE数据,因此对于代码页1200,只是按原样返回输入数据,而对于代码页1201,只需交换每个UTF-16代码单元的字节序。但对于代码页12000和12001,您必须手动转换数据(或使用第三方库,或者如果您使用的是C ++ 11或更高版本,则使用STL的内置UTF-16/32转换)
尝试这样的事情:
UINT BytesToUTF16LE(UINT CodePage, LPCSTR lpMultiByteStr, int cbMultiByte, LPWSTR lpWideCharStr, int cchWideChar)
{
UINT nchLen;
switch (nCodePage)
{
case 1200: // UTF-16LE
case 1201: // UTF-16BE
{
if ((!lpMultiByteStr) || (cbMultiByte < 0) || (cchWideChar < 0))
{
::SetLastError(ERROR_INVALID_PARAMETER);
return 0;
}
cbMultiByte /= 2;
nchLen = cbMultiByte;
if (lpWideCharStr)
{
if (cchWideChar < nchLen)
{
::SetLastError(ERROR_INSUFFICIENT_BUFFER);
return 0;
}
if (nCodePage == 1200)
CopyMemory(lpWideCharStr, lpMultiByteStr, nchLen * 2);
else
{
UINT16 pCodeUnits = (UINT16) lpMultiByteStr;
for (int i = 0; i < cbMultiByte; ++i)
{
lpWideCharStr[i] = (WCHAR) (
((pCodeUnits[i] << 8) & 0xFF00) |
((pCodeUnits[i] >> 8) & 0x00FF)
);
}
}
}
::SetLastError(0);
break;
}
case 12000: // UTF-32LE
case 12001: // UTF-32BE
{
if ((!lpMultiByteStr) || (cbMultiByte < 0) || (cchWideChar < 0))
{
::SetLastError(ERROR_INVALID_PARAMETER);
return 0;
}
PUINT32 pCodePoints = (PUINT32) lpMultiByteStr;
cbMultiByte /= 4;
nchLen = 0;
for(int i = 0; i < cbMultiByte; ++i)
{
UINT32 CodePoint = pCodePoints[i];
if (nCodePage == 12001)
{
CodePoint = (
((CodePoint >> 24) & 0x000000FF) |
((CodePoint >> 8 ) & 0x0000FF00) |
((CodePoint << 8 ) & 0x00FF0000) |
((CodePoint << 24) & 0xFF000000)
);
}
if (CodePoint < 0x10000)
{
if (lpWideCharStr)
{
if (cchWideChar < 1)
{
::SetLastError(ERROR_INSUFFICIENT_BUFFER);
return 0;
}
*lpWideCharStr++ = (WCHAR) (CodePoint & 0xFFFF);
--cchWideChar;
}
++nchLen;
}
else if (CodePoint <= 0x10FFFF)
{
if (lpWideCharStr)
{
if (cchWideChar < 2)
{
::SetLastError(ERROR_INSUFFICIENT_BUFFER);
return 0;
}
CodePoint -= 0x10000;
*lpWideCharStr++ = (WCHAR) (0xD800 + ((CodePoint >> 10) & 0x3FF));
*lpWideCharStr++ = (WCHAR) (0xDC00 + (CodePoint & 0x3FF));
cchWideChar -= 2;
}
nchLen += 2;
}
else
{
::SetLastError(ERROR_NO_UNICODE_TRANSLATION);
return 0;
}
}
::SetLastError(0);
break;
}
default:
nchLen = ::MultiByteToWideChar(nCodePage, 0, lpMultiByteStr, cbMultiByte, lpWideCharStr, cchWideChar);
break;
}
return nchLen;
}
然后你可以这样做:
UINT nchLen = BytesToUTF16LE(nCodePage, pByteArrayToConvert, ncbSzByteArrayToConvert, NULL, 0)
if ((!nchLen) && (GetLastError() != 0))
{
//Failed
}
...
BytesToUTF16LE(nCodePage, pByteArrayToConvert, ncbSzByteArrayToConvert, ...)
答案 1 :(得分:1)
MultiByteToWideChar
不提供这些转换,因为UTF-16和UTF-32不是MBCS编码。
至于如何转换它们,请按以下步骤操作:
您可以考虑使用ICU等库。