5

I have an vector<BYTE> that represents characters in a string. I want to interpret those characters as ASCII characters and store them in a Unicode (UTF-16) string. The current code assumes that the characters in the vector<BYTE> are Unicode rather than ASCII. This works fine for standard ASCII, but fails for extended ASCII characters. These characters need to be interpreted using the current code page retrieved via GetACP(). How would I go about creating a Unicode (UTF-16) string with these ASCII characters?

EDIT: I believe the solution should have something to do with the macros discussed here: http://msdn.microsoft.com/en-us/library/87zae4a3(v=vs.80).aspx I'm just not exactly sure how the actual implementation would go.

int ExtractByteArray(CATLString* pszResult, const CByteVector* pabData)
{
    // place the data into the output cstring
    pszResult->Empty();
    for(int iIndex = 0; iIndex < pabData->GetSize(); iIndex++)
        *pszResult += (TCHAR)pabData->GetAt(iIndex);

    return RC_SUCCESS;
}
4

3 回答 3

4

您应该使用MultibyteToWideChar将该字符串转换为 unicode

于 2013-02-20T15:56:13.297 回答
1

我有一个vector<BYTE>代表字符串中的字符。我想将这些字符解释为 ASCII 字符并将它们存储在 Unicode (UTF-16) 字符串中

std::vector<BYTE>只有在处理二进制数据时才应使用。在使用字符串时,请std::string改用。请注意,此std::string对象将包含将由一个或多个字节序列编码的特殊字符(因此称为多字节字符),但这些不是ASCII字符。

使用std::string后,您可以使用MultiByteToWideChar创建自己的函数,将 a std::string(包含多字节 UTF-8 字符)转换为std::wstring包含 UTF-16 编码点:

// multi byte to wide char:
std::wstring s2ws(const std::string& str)
{
    int size_needed = MultiByteToWideChar(CP_UTF8, 0, &str[0], (int)str.size(), NULL, 0);
    std::wstring wstrTo(size_needed, 0);
    MultiByteToWideChar(CP_UTF8, 0, &str[0], (int)str.size(), &wstrTo[0], size_needed);
    return wstrTo;
}
于 2013-02-20T16:16:25.417 回答
0

既然您使用的是 MFC,那就让我们CString来完成这项工作。

于 2013-02-20T16:27:23.617 回答