标签: wstring

对C++的std :: wstring,UTF-16,UTF-8以及在Windows GUI中显示字符串感到困惑

我正在为Windows开发一个仅限英语的C++程序,我们被告知"总是使用std :: wstring",但似乎团队中的任何人都没有太多理解.

我已经阅读了标题为"std :: wstring VS std :: string.的问题.这非常有用,但我仍然不太明白如何将所有这些信息应用到我的问题中.

我正在处理的程序在Windows GUI中显示数据.该数据以XML格式保存.我们经常使用XSLT将XML转换为HTML或XSL:FO以用于报告目的.

根据我所读到的内容,我的感觉是HTML应编码为UTF-8.我对GUI开发知之甚少,但我读到的一点点表明GUI内容都是基于UTF-16编码的字符串.

我想知道这会让我离开的地方.假设我们认为所有持久化数据都应该是UTF-8编码的XML.这是否意味着为了在UI组件中显示持久化数据,我应该真正执行某种明确的UTF-8到UTF-16转码过程吗?

我怀疑我的解释可以使用澄清,所以如果你有任何问题,我会尝试提供.

c++ unicode utf-8 utf-16 wstring

17
推荐指数
3
解决办法
2万
查看次数

将wstring转换为以UTF-8编码的字符串

我需要在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语言环境正常工作.

c++ string utf-8 wstring

17
推荐指数
3
解决办法
3万
查看次数

将std :: wstring转换为QString的最佳方法

我目前正在开发一个更大的项目,其中"逻辑"在标准C++中实现,所有字符串都在处理,std::wstringUI部分使用Qt实现,因此必然QString(奖励问题:这是真的吗?).

连接这两个世界的最佳方式是什么?

我知道我可以用类似的东西

std::wstring wideString;
QString qtString = QString::fromStdWString(wideString);
Run Code Online (Sandbox Code Playgroud)

但我想知道是否有更好的方式,涉及更少的打字.一个用户定义的操作员出现在我的脑海中,但我没有足够的经验来解决这个问题.

如果有人能指出我正确的方向,我会很高兴的.

c++ qstring qt wstring

17
推荐指数
1
解决办法
1万
查看次数

将CString转换为std :: wstring

我怎样才能转换CStringstd::wstring

c++ string wstring

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

15
推荐指数
1
解决办法
1万
查看次数

为什么我不能读取超过4094个字符的UTF-16文件?

一些信息:

  • 我只在Linux上试过这个
  • 我和GCC(7.2.0)和Clang(3.8.1)一起尝试过
  • 根据我的理解,它需要C++ 11或更高版本

运行时会发生什么

我得到预期的字符串"abcd"重复,直到它达到4094个字符的位置.之后所有输出都是这个标志"?" 直到文件结束.

我怎么看待这个?

我认为这不是预期的行为,它必定是某个地方的错误.

你可以测试的代码:

#include <iostream>
#include <fstream>
#include <locale>
#include <codecvt>

void createTestFile() {
  std::ofstream file ("utf16le.txt", std::ofstream::binary);
  if (file.is_open()) {
    uint16_t bom = 0xFEFF; // UTF-16 little endian BOM
    uint64_t abcd = 0x0064006300620061; // UTF-16 "abcd" string
    file.write((char*)&bom,2);
    for (size_t i=0; i<2000; i++) {
      file.write((char*)&abcd,8);
    }
    file.close();
  }
}

int main() {
  //createTestFile(); // uncomment to make the test file

  std::wifstream file;
  std::wstring line;

  file.open("utf16le.txt");
  file.imbue(std::locale(file.getloc(), new std::codecvt_utf16<wchar_t, 0x10ffff, std::consume_header>));
  if …
Run Code Online (Sandbox Code Playgroud)

c++ linux utf-16 wstring wifstream

13
推荐指数
1
解决办法
399
查看次数

在C++中处理UTF-8

要了解C++是否是我的项目的正确语言,我想测试UTF-8功能.根据参考资料,我建立了这个例子:

#include <string>
#include <iostream>

using namespace std;

int main() {
    wstring str;
    while(getline(wcin, str)) {
        wcout << str << endl;
        if(str.empty()) break;
    }

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

但是当我输入一个UTF-8字符时,它行为不端:

$ > ./utf8 
Hello
Hello
für
f
$ >
Run Code Online (Sandbox Code Playgroud)

它不仅不会打印ü,而且会立即退出.gdb告诉我没有崩溃,但正常退出,但我发现很难相信.

c++ linux stl utf-8 wstring

12
推荐指数
2
解决办法
1万
查看次数

如何获取std :: wstring的字节大小?

std::wstring用作我的Unicode样式字符串.现在我想得到一个字节大小wstring.如果我使用size()方法wstring,我只得到我的字符总数wstring.但是字节应该是size()*2.是否有正式的方法来获得这个字节大小?我不想在我的程序中使用size()*2 .....

我想RegSetValueExW用作最后一个参数.

c++ string size winapi wstring

11
推荐指数
2
解决办法
1万
查看次数

C++中的UTF-8兼容性

我正在编写一个程序,需要能够使用所有语言的文本.我的理解是UTF-8将完成这项工作,但我遇到了一些问题.

我是否可以说UTF-8可以存储char在C++中?如果是这样,为什么我在使用程序时会收到以下警告char,string并且stringstream:warning C4566: character represented by universal-character-name '\uFFFD' cannot be represented in the current code page (1252).(我使用时没有出现错误wchar_t,wstring并且wstringstream.)

另外,我知道UTF是可变长度的.当我使用atsubstr字符串方法时,我会得到错误的答案?

c++ unicode wchar-t utf-8 wstring

11
推荐指数
2
解决办法
1万
查看次数

求istreambuf_iterator <wchar_t>澄清,阅读Unicode字符的完整文本文件

在Scott Meyers的"Effective STL"一书中,有一个将整个文本文件读入std :: string对象的好例子:

std::string sData; 

/*** Open the file for reading, binary mode ***/
std::ifstream ifFile (“MyFile.txt”, std::ios_base::binary); // Open for input, binary mode

/*** Read in all the data from the file into one string object ***/
sData.assign (std::istreambuf_iterator <char> (ifFile),
              std::istreambuf_iterator <char> ());
Run Code Online (Sandbox Code Playgroud)

请注意,它以8字节字符的形式读取.这非常有效.最近虽然我需要读取包含Unicode文本的文件(即每个字符两个字节).但是,当我尝试(天真地)更改它以将数据从Unicode文本文件读取到std :: wstring对象时,如下所示:

std::wstring wsData; 

/*** Open the file for reading, binary mode ***/
std::wifstream ifFile (“MyFile.txt”, std::ios_base::binary); // Open for input, binary mode

/*** Read in all the data from the file …
Run Code Online (Sandbox Code Playgroud)

c++ unicode istream-iterator wstring wifstream

11
推荐指数
1
解决办法
2544
查看次数

标签 统计

c++ ×10

wstring ×10

utf-8 ×4

string ×3

unicode ×3

linux ×2

utf-16 ×2

wifstream ×2

istream-iterator ×1

null ×1

qstring ×1

qt ×1

size ×1

stl ×1

wchar-t ×1

winapi ×1