簡體   English   中英

如何將std :: stringstream轉換為std :: wstring

[英]How to convert std::stringstream to std::wstring

我的問題是如何在wstringwstringstream轉換c ++ stringstream

stringstream fileSearch;
fileSearch<<fileOutput.str();
fileSearch<<"*.jpg";
cout<<fileSearch.str()<<endl;

這是我的代碼。 我想將此fileSearch字符串流轉換為wstring ...有人可以幫我請c ++示例代碼...我想在

int numOfFiles(wstring searchPath);

這個功能...

std::stringstream始終可以轉換為std::string ,因此問題就在於如何將std::string轉換為std::wstring


如果窄字符串編碼點是寬字符串編碼點的子集,則可以簡單地將數據復制到:

const std::string s = ...;
const std::wstring ws( s.begin(), s.end() );

當寬字符串是UTF-16或UTF-32編碼時,這適用於原始ASCII及其擴展名Latin-1。

實際上,這意味着該簡單的數據復制方案適用於:

  • Windows 1的西方安裝中的Latin-1,因為Latin-1是Windows ANSI Western的子集。

  • 在其他Windows安裝環境和Unix-land中,ASCII是因為默認的系統窄編碼(通常)不是Latin-1的擴展。


當窄字符串編碼點不是寬字符串編碼點的子集時,必須采用一些更有效的轉換。

std::string的編碼是語言環境的窄文本編碼,並且不包含嵌入的零字節時,以下代碼有效:

#include <iostream>
#include <locale>       // std::locale
#include <locale.h>     // setlocale
#include <stdexcept>    // std::runtime_error
#include <stdlib.h>     // mbstowcs
#include <string>
using namespace std;

auto hopefully( const bool condition ) -> bool { return condition; }
auto fail( const string& message ) -> bool { throw runtime_error( message ); }

auto widened( const string& s, locale const& loc = locale() )
    -> wstring
{
    const int n = s.length();
    if( n == 0 ) { return L""; }

    const int max_wide_encoding_values = (sizeof( wchar_t ) == 2? 2 : 1);
    wstring ws( max_wide_encoding_values*s.length(), L'\0' );

    const auto n_characters_stored = mbstowcs( &ws[0], &s[0], ws.size() );
    hopefully( n_characters_stored != -1 )
        || fail( "mbstowcs failed" );
    ws.resize( n_characters_stored );
    return ws;
}

auto operator<<( wostream& stream, const string& s )
    -> wostream&
{ return stream << s.c_str(); }

auto main() -> int
{
    setlocale( LC_ALL, "" );
    locale::global( locale( "" ) );

    const wstring ws = widened( "Blåbærsyltetøy." );
    for( const wchar_t wc : ws )
    {
        wcout << int( wc ) << ' ';
    }
    wcout << endl;
    wcout << L"Should be 'Blåbærsyltetøy'." << endl;
    wcout << L"Is '" << ws << L"'." << endl;
}

在Ubuntu(在Windows的VirtualBox中)中,輸出正常:

alf@devubuntu32:~/host/dev/explore/_/so/0244$ 
alf@devubuntu32:~/host/dev/explore/_/so/0244$ 
66 108 229 98 230 114 115 121 108 116 101 116 248 121 46 
Should be 'Blåbærsyltetøy'.
Is 'Blåbærsyltetøy.'.
alf@devubuntu32:~/host/dev/explore/_/so/0244$ ▯

在Windows中,它是1,必須加入一些修正,使寬流輸出工作:

#include <io.h>
#include <fcntl.h>
#include <stdio.h>

static const bool _ = []() -> bool
{
    const int fd = _fileno( stdout );
    _setmode( fd, _isatty( fd )? _O_WTEXT : _O_U8TEXT );
    return true;
}();

然后在Windows中使用Visual C ++,輸出為

H:\dev\explore\_\so\0244>
iofix.cpp
foo.cpp
Generating Code...

H:\dev\explore\_\so\0244>
66 108 229 98 230 114 115 121 108 116 101 116 248 121 46
Should be 'Blåbærsyltetøy'.
Is 'Blåbærsyltetøy.'.

H:\dev\explore\_\so\0244>_

但是,在Windows中使用MinGW g ++時,默認輸出不正確:

H:\dev\explore\_\so\0244>

H:\dev\explore\_\so\0244>
66 108 195 165 98 195 166 114 115 121 108 116 101 116 195 184 121 46
Should be 'Blåbærsyltetøy'.
Is 'Blåbærsyltetøy.'.

H:\dev\explore\_\so\0244>_

原因是默認的g ++ C ++執行字符集為UTF-8,這不是Windows中默認用戶的語言環境指定的窄文本編碼。 一個簡單的解決方法是將正確的執行字符集指定為g ++。 但是,實際上只有支持這些選項的g ++發行版才有可能,例如Nuwen發行版不支持。


1 )在Unix領域中按原樣工作,因為全局C ++語言環境已設置為用戶的默認語言環境。

在C ++ 11中,您也可以使用它將字符串流轉換為wstring:

std::wstring stringStream2wstring(std::stringstream& strs)
{
    std::string str = strs.str();
    typedef std::codecvt_utf8<wchar_t> convert_type;
    std::wstring_convert<convert_type, wchar_t> converter;
    return converter.from_bytes(str);
}

注意:該示例使用std :: codecvt_utf8

一個簡短而簡單的C ++函數,用於將字符串流轉換為wstring:

std::wstring convertToWString(std::stringstream& from)
{
    std::wstring to;
    string stdString = from.str();
    return to.assign(stdString.begin(), stdString.end());
}

注意:此代碼僅限於值的ASCII子集

#include <boost\lexical_cast.hpp>
stringstream ss;    
std::wstring convertedStr = boost::lexical_cast<std::wstring>(ss);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM