相关疑难解决方法(0)

utfcpp和Win32广泛的API

是否好/安全/可以使用微小的utfcpp库将我从宽Windows API(FindFirstFileW等)返回的所有内容转换为使用utf16to8的有效UTF8表示?

我想在内部使用UTF8,但是无法获得正确的输出(在另一次转换或普通cout之后通过wcout).普通的ASCII字符当然可以工作,但是ñä搞砸了.

还是有更简单的选择?

谢谢!

更新:感谢Hans(下面),我现在可以通过Windows API进行简单的UTF8 < - > UTF16转换.双向转换有效,但UTF16字符串中的UTF8有一些额外的字符可能会在以后引起我的麻烦......).我将在这里分享它纯粹的友好:)):

// UTF16 -> UTF8 conversion
std::string toUTF8( const std::wstring &input )
{
    // get length
    int length = WideCharToMultiByte( CP_UTF8, NULL,
                                      input.c_str(), input.size(),
                                      NULL, 0,
                                      NULL, NULL );
    if( !(length > 0) )
        return std::string();
    else
    {
        std::string result;
        result.resize( length );

        if( WideCharToMultiByte( CP_UTF8, NULL,
                                 input.c_str(), input.size(),
                                 &result[0], result.size(),
                                 NULL, NULL ) > 0 )
            return result;
        else
            throw std::runtime_error( "Failure to execute toUTF8: conversion failed." …
Run Code Online (Sandbox Code Playgroud)

c++ winapi utf-8 utf-16 wide-api

3
推荐指数
1
解决办法
2359
查看次数

标签 统计

c++ ×1

utf-16 ×1

utf-8 ×1

wide-api ×1

winapi ×1