【问题标题】:Serialize string to UTF w/o BOM将字符串序列化为 UTF w/o BOM
【发布时间】:2014-02-10 21:05:38
【问题描述】:

我正在尝试将 unicode 字符串序列化编码为 UTF-8 w/o BOM 文件。由于某种原因,下面的代码给出了错误的输出。

static void MyWriteFile(HANDLE hFile, PTCHAR pszText, int cchLen, BOOL bAsUnicode)
{
  DWORD dwBytes;
  size_t utf8len = WideCharToMultiByte(CP_UTF8, 0, pszText, -1, NULL, 0, NULL, NULL);
  PCHAR pszConverted = (PCHAR)LocalAlloc(LPTR, utf8len);

  WideCharToMultiByte(CP_UTF8, 0, pszText, utf8len, pszConverted, utf8len, 0, 0);
  WriteFile(hFile, pszConverted, utf8len, &dwBytes, NULL);
}

【问题讨论】:

    标签: c++ c encoding utf-8


    【解决方案1】:
    WideCharToMultiByte(CP_UTF8, 0, pszText, utf8len, pszConverted, utf8len, 0, 0);
    

    WideCharToMultiByte(cchWideChar)的第四个参数是输入字符串的大小。您应该将其保留为 -1,因为它是空终止的。否则你的输出缓冲区可能不够大,会包含太多数据。

    【讨论】:

      猜你喜欢
      • 2016-05-06
      • 2013-07-26
      • 2011-08-01
      • 2021-01-08
      • 2010-12-10
      • 2014-06-16
      • 2019-03-08
      • 2020-05-20
      相关资源
      最近更新 更多