我需要在wstring和string之间进行转换.我想,使用codecvt facet应该可以解决问题,但它似乎不适用于utf-8语言环境.
我的想法是,当我将utf-8编码文件读取到字符时,一个utf-8字符被读入两个普通字符(这就是utf-8的工作原理).我想从我的代码中使用的库的wstring表示创建这个utf-8字符串.
有谁知道怎么做?
我已经尝试过了:
locale mylocale("cs_CZ.utf-8");
mbstate_t mystate;
wstring mywstring = L"??žýáí";
const codecvt<wchar_t,char,mbstate_t>& myfacet =
use_facet<codecvt<wchar_t,char,mbstate_t> >(mylocale);
codecvt<wchar_t,char,mbstate_t>::result myresult;
size_t length = mywstring.length();
char* pstr= new char [length+1];
const wchar_t* pwc;
char* pc;
// translate characters:
myresult = myfacet.out (mystate,
mywstring.c_str(), mywstring.c_str()+length+1, pwc,
pstr, pstr+length+1, pc);
if ( myresult == codecvt<wchar_t,char,mbstate_t>::ok )
cout << "Translation successful: " << pstr << endl;
else cout << "failed" << endl;
return 0;
Run Code Online (Sandbox Code Playgroud)
它为cs_CZ.utf-8语言环境返回'failed',并且对cs_CZ.iso8859-2语言环境正常工作.
你好我有一个使用Windows Forms的旧C#应用程序编写得很差.我最近不得不在应用程序中进行一些大的更改,现在我面临着非常奇怪的问题.
该应用程序首先被设计为在主窗口OnLoad方法中加载数据(可怕的想法,我知道),然后重构,以便不引人注意地在另一个线程中加载数据.该应用程序使用BindingListView(http://blw.sourceforge.net/),这是IBindingListView接口的非常简单的实现.
在我对应用程序所做的更改之前一切正常.应用程序在加载所有数据时启动,并且没有任何问题(在等待应用程序加载数据时忽略了糟糕的用户体验).
但是当我更改应用程序以在另一个线程中动态加载数据时(网格首先显示为空,然后将数据加载到它),开始弹出一些空行,这些行不在绑定到网格的ObservableCollection中.
我设法跟踪这个调用堆栈的新添加的行:
Equin.ApplicationFramework.BindingListView.dll!Equin.ApplicationFramework.AggregateBindingListView<BusinessLogic.DataEntries.TrackEntry>.AddNew() Line 295 C#
Equin.ApplicationFramework.BindingListView.dll!Equin.ApplicationFramework.AggregateBindingListView<BusinessLogic.DataEntries.TrackEntry>.System.ComponentModel.IBindingList.AddNew() Line 1617 + 0x8 bytes C#
System.Windows.Forms.dll!System.Windows.Forms.BindingSource.AddNew() + 0xbd bytes
System.Windows.Forms.dll!System.Windows.Forms.CurrencyManager.AddNew() + 0x26 bytes
System.Windows.Forms.dll!System.Windows.Forms.DataGridView.DataGridViewDataConnection.AddNew() + 0x86 bytes
System.Windows.Forms.dll!System.Windows.Forms.DataGridView.DataGridViewDataConnection.OnNewRowNeeded() + 0x24 bytes
System.Windows.Forms.dll!System.Windows.Forms.DataGridView.OnRowEnter(ref System.Windows.Forms.DataGridViewCell dataGridViewCell = null, int columnIndex = 0, int rowIndex = 0, bool canCreateNewRow, bool validationFailureOccurred) + 0x10a bytes
System.Windows.Forms.dll!System.Windows.Forms.DataGridView.SetCurrentCellAddressCore(int columnIndex = 0, int rowIndex = 0, bool setAnchorCellAddress, bool validateCurrentCell, bool throughMouseClick = false) + 0x59f bytes
System.Windows.Forms.dll!System.Windows.Forms.DataGridView.SetAndSelectCurrentCellAddress(int columnIndex = 0, int rowIndex …Run Code Online (Sandbox Code Playgroud) 在我的应用程序中,我使用 ICU UnicodeString 来存储我的字符串。由于我使用一些与 ICU 不兼容的库,因此我需要将 UnicodeString 转换为其平台相关的表示形式。
基本上我需要做的是逆向处理形式创建新的 UnicodeString 对象 - new UnicodeString(“在系统区域设置中编码的字符串”)。
我发现了这个主题 - 所以我知道它可以通过使用 stringstream 来完成。
所以我的答案是,是否可以通过其他更简单的方式来完成,而不使用 stringstream 进行转换?