【问题标题】:How to read a std::wstring written in linux into windows如何将用linux编写的std :: wstring读入windows
【发布时间】:2023-03-16 17:24:01
【问题描述】:

我们有一个可以在 Windows 和 Linux 上运行的程序。它以二进制形式将 std::wstrings 写入文件。我们需要能够读取从 linux 写入到 windows 的文件。我们将字符串写成 wchar_t 的列表。在 linux 上,每个 wchar_t 占用 4 个字节。在 Windows 上,每个 wchar_t 占用 2 个字节。

在将linux写入的文件读入windows时,如何将4字节的wchar_t放入2字节的wchar_t中呢?

谢谢, 亚当

【问题讨论】:

    标签: c++ windows linux cross-platform wstring


    【解决方案1】:

    您可以使用UTF8-CPP 轻松将文件从 UTF-32 转换为 UTF-16:

    #include <fstream>
    #include <iterator>
    #include <utf8.h>
    
    int main(int argc, char** argv) {
    
        std::ifstream file("source.txt");
        std::string   intermediate;
        std::wstring  result;
    
        utf8::utf32to8(std::istreambuf_iterator<char>(file),
                       std::istreambuf_iterator<char>(),
                       std::back_inserter(intermediate));
    
        utf8::utf8to16(intermediate.begin(),
                       intermediate.end(),
                       std::back_inserter(result));
    
    }
    

    很遗憾,没有utf8::utf32to16,但也许应该有。

    【讨论】:

      【解决方案2】:

      假设 Linux 代码以 UTF-32 格式输出,您必须编写一些代码将字符串转换为 UTF-16,这是 Windows 上使用的 Unicode 编码。 wstring 帮不了你。转换为 UTF-16 后,您可以在 Windows 上使用 wchar_t 将其存储在 wstring 中。

      【讨论】:

      猜你喜欢
      • 2011-05-02
      • 2011-05-07
      • 1970-01-01
      • 2010-12-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-16
      • 2013-05-22
      相关资源
      最近更新 更多