我在Linux系统上使用Qt/C++.我需要将一个QLineEdit文本转换为一个文本std::wstring并将其写入std::wofstream.它适用于ascii字符串,但是当我输入任何其他字符(阿拉伯语或乌兹别克语)时,文件中没有任何内容.(文件大小为0字节).
这是我的代码:
wofstream customersFile;
customersFile.open("./customers.txt");
std::wstring ws = lne_address_customer->text().toStdWString();
customersFile << ws << ws.length() << std::endl;
Run Code Online (Sandbox Code Playgroud)
John Smith在行编辑中输入的输出是John Smith10.但对于unicode字符串,什么都没有.
首先我认为这是一个问题QString::toStdWString(),但customersFile << ws.length();写出所有字符串的正确长度.所以我想我在写wstring文件时做错了.[?]
编辑:
我在eclipse中再次写它.并用g ++ 4.5编译它.结果是一样的:
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main()
{
cout << "" << endl; // prints
wstring ws = L"????"; // this is an Arabic "Hello"
wofstream wf("new.txt");
if (!wf.bad())
wf << ws;
else
cerr << …Run Code Online (Sandbox Code Playgroud) 我正在使用boost来序列化文本存档和std :: wstring变量.如果我切换到std :: string它工作得很好但是当我使用wstring时,我只得到一个序列化的字符.为什么?
std::wstring text;
template<class Archive> void serialize(Archive &ar, const unsigned int version)
{
ar & text;
}
...
std::ostringstream stream;
boost::archive::text_oarchive archive(stream);
archive << params;
...
stream.str()
Run Code Online (Sandbox Code Playgroud) 我知道加载unicode是一个有点劳累的观点,但我看不出如何将呈现给别人的解决方案应用到我的特定问题中.
我有一个Win7/C++/DirectX9 GUI库,可以将文本呈现到屏幕上.我之前从未遇到过任何问题,因为它只能用于西欧语言.现在我必须和匈牙利人一起使用它,这让我很头疼!我特别的问题是加载那种语言中的特殊字符.
举个例子,FELNŐTTEKNEK,意思是ADULT.
如果我将此字符串硬编码到我的应用程序中,它会正确呈现:
guiTitle->SetText( L"FELN?TTEKNEK" );
Run Code Online (Sandbox Code Playgroud)
这将字符串存储为std :: wstring,使用ID3DXFont :: DrawTextW()呈现它.它还证明了我选择的字体Futura CE能够渲染特殊字符(CE =中欧).
到现在为止还挺好.接下来,我只想加载文本文件中的文本.没什么大不了.但结果很糟糕!特殊Ő被另一个字符取代,主要是Å甚至两个字符,如Å(第二个通常是不可打印的)
我确保通过输入文本文件编码为UTF-8并且天真地试图加载它:
wifstream f("data/language.ini");
wstring w;
getline( f, w );
guiTitle->SetText( w );
Run Code Online (Sandbox Code Playgroud)
不知何故,我仍然在争抢它.我加载为UTF-8吗?有没有办法确保这个?我只需要确保我在文本编辑器中显示带有文本的宽字符串.
最感激的任何帮助.
硅
这是代码:
std::ofstream f("file1.txt");
f<<"123"<<std::endl<<"456"; //(*1)
/*std::stringstream ordinary_strstream; This works too
ordinary_strstream<<"123"<<'\n'<<"456";
f<<ordinary_strstream.str();*/
std::wstringstream s;
s<<L"123"<<std::endl<<L"456"; //(*2)
s<<L"123"<<L"\n"<<L"456"; //(*3)
s<<"123"<<WCHAR(13)<<WCHAR(10)<<"456";//(*4)
HANDLE h =CreateFileW(L"file2.txt", GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
ULONG l;
WriteFile(h, s.str().c_str(), s.str().length() * 2, &l, NULL);
Run Code Online (Sandbox Code Playgroud)
在(*1)的情况下有一个换行符,在(*2)和(*3)中我看不到file2.txt中的换行符.在(*3)中有换行符.我用它notepad.exe来浏览.十六进制编辑器0x0D仅显示无字节0x0A.
我应该如何正确地将换行符放在unicode文本文件中?谢谢.
出于调试原因,我想将一个unsigned char附加到wstring.
但是,我没有找到将unsigned char转换为wstring的函数,所以我无法追加它.
编辑:到目前为止发布的解决方案并没有真正做到我需要的.我想将0转换为"0".到目前为止,解决方案将0转换为0字符,但不转换为"0"字符串.
有人可以帮忙吗?
谢谢.
unsigned char SomeValue;
wstring sDebug;
sDebug.append(SomeValue);
Run Code Online (Sandbox Code Playgroud) 我想转换wstring为u16stringC ++。
我可以转换wstring为字符串,也可以反转。但我不知道如何转换为u16string。
u16string CTextConverter::convertWstring2U16(wstring str)
{
int iSize;
u16string szDest[256] = {};
memset(szDest, 0, 256);
iSize = WideCharToMultiByte(CP_UTF8, NULL, str.c_str(), -1, NULL, 0,0,0);
WideCharToMultiByte(CP_UTF8, NULL, str.c_str(), -1, szDest, iSize,0,0);
u16string s16 = szDest;
return s16;
}
Run Code Online (Sandbox Code Playgroud)
WideCharToMultiByte(CP_UTF8,NULL,str.c_str(),-1,szDest,iSize,0,0);中的错误szDest。u16string无法与使用的原因LPSTR。
如何解决此代码?
我试图使用L“ string”,但是它不起作用。
#include <iostream>
using namespace std;
int main(){
wstring wstr = L"??";//[Error] converting to execution character set: Illegal byte sequence
wcout<<wstr<<endl;
}
Run Code Online (Sandbox Code Playgroud)
使用wcin并输入?? 工作正常。
#include <iostream>
using namespace std;
int main(){
wstring wstr;
wcin>>wstr;//Input Chinese is OK
wcout<<wstr<<endl;
}
Run Code Online (Sandbox Code Playgroud)
如何初始化或分配?w?
编辑:我尝试了一些在线编译器。它们都可以编译,但是都输出“ ??”。
例如cpp.sh jdoodle onlinegdb repl.it
编辑2:我安装了g ++ i868 MinGW-W64 8.1.0。使用Visual Studio将cpp文件保存为utf8格式。然后使用命令行对其进行编译。它仍然不输出任何内容。
我用C++编写了一个小应用程序.UI中有一个ListBox.我想使用ListBox的选定项目作为算法,我只能使用wstrings.
总而言之,我有两个问题: - 我可以如何转换我的
String^ curItem = listBox2->SelectedItem->ToString();
Run Code Online (Sandbox Code Playgroud)
到一个wstring测试?
- 代码中的^是什么意思?
非常感谢!
我想从 .dll 字符串表中读取 utf-8 测试。像这样的东西
LPWSTR nnW;
LoadStringW(hMod, id, nnW, MAX_PATH);
Run Code Online (Sandbox Code Playgroud)
之后,我想转换LPWSTR nnW到std::wstring nnWstring。我是这样试的:LPWSTR nnW; LoadStringW(hMod, id, nnW, MAX_PATH);
const int length = MultiByteToWideChar(CP_UTF8,
0, // no flags required
(LPCSTR)nnW,
-1, // automatically determine length
NULL,
0);
std::wstring nnWstring(length, L'\0');
if (!MultiByteToWideChar(CP_UTF8,
0,
(LPCSTR)nnW,
-1,
&nnWstring[0],
length))
MessageBoxW(NULL, (LPCWSTR)nnWstring.c_str(), L"wstring", MB_OK | MB_ICONERROR);
Run Code Online (Sandbox Code Playgroud)
之后在MessageBoxW 中只显示第一个字母。
我想知道,是否有人可以向我解释 vector.insert() 方法中的第二个参数:
迭代器插入(迭代器位置,const value_type& val);
例如,我有一个 wstring 类型的向量,我想在给定位置插入一个 wstring。我已经弄清楚如何使用迭代器设置位置:
wstring word = "test";
int insertion_pos = 3;
iterator it = words.begin();
words.insert( it + insertion_pos, word );
Run Code Online (Sandbox Code Playgroud)
但是第二个论点呢?如何将 wstring 对象传递给 insert() 方法?非常感谢。
干杯,
马丁
完整示例:
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <wchar.h>
#include <vector>
using namespace std;
int main(void) {
// Initialize the vecor with three words.
vector<wstring> words;
wstring word1 = "FirstWord"; // Error msg: no viable conversion from 'const char [10]' to 'wstring' (aka
// 'basic_string<wchar_t>') …Run Code Online (Sandbox Code Playgroud)