从ASCII转换为Unicode字符代码(FreeType2)

Tom*_*lla 4 c++ unicode ascii freetype

我在我的一个项目中使用FreeType2.为了呈现一个字母,我需要提供一个Unicode双字节字符代码.但代码读取的字符代码是ASCII单字节格式.对于低于128的字符代码(字符代码相同),它没有问题,但是其他128不匹配.例如:

ASCII中的'a'是0x61,Unicode中的'a'是0x0061 -
ASCII 中的'' 很好'''是0xB9,Unicode中的'''是0x0105 - 完全不同

我试图在那里使用WinAPI函数,但我一定是做错了.这是一个示例:

unsigned char szTest1[] = "?ó??"; //ASCII format
wchar_t* wszTest2;
int size = MultiByteToWideChar(CP_UTF8, 0, (char*)szTest1, 4, NULL, 0);
printf("size = %d\n", size);
wszTest2 = new wchar_t[size];
MultiByteToWideChar(CP_UTF8, 0, (char*)szTest1, 4, wszTest2, size);
printf("HEX: %x\n", wszTest2[0]);
delete[] wszTest2;
Run Code Online (Sandbox Code Playgroud)

我期待创建一个新的宽字符串,最后没有NULL.但是,大小变量总是等于0.任何想法我做错了什么?或者也许有一种更简单的方法来解决问题?

Mr.*_*C64 6

"纯"ASCII字符集限制在0-127(7位)范围内.具有最高有效位设置的8位字符(即范围128-255中的那些)不是唯一定义的:它们的定义取决于代码页.所以,你的角色?(与反尾形符拉丁小写字母A)由值表示0xB9在一个特定的代码页,这应该是Windows的1250.在其他代码页中,该值0xB9不同的字符相关联(例如,在Windows 1252代码页中,0xB9与字符相关联¹,即上标数字1).

要使用Windows Win32 API将字符从特定代码页转换为Unicode UTF-16,您可以使用MultiByteToWideChar,指定正确的代码页(不是 CP_UTF8在您的问题中的代码中写入;实际上,CP_UTF8标识Unicode UTF-8) .您可能希望尝试指定1250(ANSI中欧;中欧(Windows))作为正确的代码页标识符.

如果您可以在代码中访问ATL,则可以使用ATL字符串转换助手类的便利,例如CA2W,它包含MultiByteToWideChar(RAII类中的调用和内存分配; 例如:

#include <atlconv.h> // ATL String Conversion Helpers
// 'test' is a Unicode UTF-16 string.
// Conversion is done from code-page 1250
// (ANSI Central European; Central European (Windows))
CA2W test("?ó??", 1250);
Run Code Online (Sandbox Code Playgroud)

现在您应该可以test在Unicode API中使用字符串了.

如果您无法访问ATL或想要基于C++ STL的解决方案,您可能需要考虑以下代码:

///////////////////////////////////////////////////////////////////////////////
//
// Modern STL-based C++ wrapper to Win32's MultiByteToWideChar() C API.
//
// (based on http://code.msdn.microsoft.com/windowsdesktop/C-UTF-8-Conversion-Helpers-22c0a664)
//
///////////////////////////////////////////////////////////////////////////////

#include <exception>    // for std::exception
#include <iostream>     // for std::cout
#include <ostream>      // for std::endl
#include <stdexcept>    // for std::runtime_error
#include <string>       // for std::string and std::wstring
#include <Windows.h>    // Win32 Platform SDK

//-----------------------------------------------------------------------------
// Define an exception class for string conversion error.
//-----------------------------------------------------------------------------
class StringConversionException 
    : public std::runtime_error
{
public:
    // Creates exception with error message and error code.
    StringConversionException(const char* message, DWORD error)
        : std::runtime_error(message)
        , m_error(error)
    {}

    // Creates exception with error message and error code.
    StringConversionException(const std::string& message, DWORD error)
        : std::runtime_error(message)
        , m_error(error)
    {}

    // Windows error code.
    DWORD Error() const
    {
        return m_error;
    }

private:
    DWORD m_error;
};

//-----------------------------------------------------------------------------
// Converts an ANSI/MBCS string to Unicode UTF-16.
// Wraps MultiByteToWideChar() using modern C++ and STL.
// Throws a StringConversionException on error.
//-----------------------------------------------------------------------------
std::wstring ConvertToUTF16(const std::string & source, const UINT codePage)
{
    // Fail if an invalid input character is encountered
    static const DWORD conversionFlags = MB_ERR_INVALID_CHARS;

    // Require size for destination string
    const int utf16Length = ::MultiByteToWideChar(
        codePage,           // code page for the conversion
        conversionFlags,    // flags
        source.c_str(),     // source string
        source.length(),    // length (in chars) of source string
        NULL,               // unused - no conversion done in this step
        0                   // request size of destination buffer, in wchar_t's
        );
    if (utf16Length == 0) 
    {
        const DWORD error = ::GetLastError();
        throw StringConversionException(
            "MultiByteToWideChar() failed: Can't get length of destination UTF-16 string.",
            error);
    }

    // Allocate room for destination string
    std::wstring utf16Text;
    utf16Text.resize(utf16Length);

    // Convert to Unicode UTF-16
    if ( ! ::MultiByteToWideChar(
        codePage,           // code page for conversion
        0,                  // validation was done in previous call
        source.c_str(),     // source string
        source.length(),    // length (in chars) of source string
        &utf16Text[0],      // destination buffer
        utf16Text.length()  // size of destination buffer, in wchar_t's
        )) 
    {
        const DWORD error = ::GetLastError();
        throw StringConversionException(
            "MultiByteToWideChar() failed: Can't convert to UTF-16 string.",
            error);
    }

    return utf16Text;
}

//-----------------------------------------------------------------------------
// Test.
//-----------------------------------------------------------------------------
int main()
{
    // Error codes
    static const int exitOk = 0;
    static const int exitError = 1;

    try 
    {
        // Test input string:
        //
        // ? - LATIN SMALL LETTER A WITH OGONEK
        std::string inText("x - LATIN SMALL LETTER A WITH OGONEK");
        inText[0] = 0xB9;

        // ANSI Central European; Central European (Windows) code page
        static const UINT codePage = 1250;

        // Convert to Unicode UTF-16
        const std::wstring utf16Text = ConvertToUTF16(inText, codePage);

        // Verify conversion.
        //  ? - LATIN SMALL LETTER A WITH OGONEK
        //  --> Unicode UTF-16 0x0105
        // http://www.fileformat.info/info/unicode/char/105/index.htm
        if (utf16Text[0] != 0x0105) 
        {
            throw std::runtime_error("Wrong conversion.");
        }
        std::cout << "All right." << std::endl;
    }
    catch (const StringConversionException& e)
    {
        std::cerr << "*** ERROR:\n";
        std::cerr << e.what() << "\n";
        std::cerr << "Error code = " << e.Error();
        std::cerr << std::endl;
        return exitError;
    }
    catch (const std::exception& e)
    {
        std::cerr << "*** ERROR:\n";
        std::cerr << e.what();
        std::cerr << std::endl;
        return exitError;
    }
    return exitOk;
}

///////////////////////////////////////////////////////////////////////////////
Run Code Online (Sandbox Code Playgroud)


shf*_*301 5

CodePage对参数MultiByteToWideChar是错误的.Utf-8与ASCII不同.您应该使用CP_ACP哪个告诉当前系统代码页(与ASCII不同 - 请参阅Unicode,UTF,ASCII,ANSI格式差异)

大小为零的可能性最大,因为您的测试字符串不是有效的Utf-8字符串.

对于几乎所有Win32函数,您可以在函数无法获取详细的错误代码后调用GetLastError(),因此调用它也会为您提供更多详细信息.