我如何HTML- / URL-编码包含Unicode字符的std :: wstring?

时间:2010-07-21 13:58:42

标签: c++ html url unicode utf-8

我还有一个问题。如果我有一个std :: wstring看起来像这样:

  

ドイツ语で検索していてこちらのサイトにたどり着きました。

我怎么可能将它编码为URL(%nn n = 0-9,a-f)到:

  

%E3%83%89%E3%82%A4%E3%83%84%E8%AA%9E%E3%81%A7%E6%A4%9C%E7%B4%A2%E3%81% 97%E3%81%A6%E3%81%84%E3%81%A6%E3%81%93%E3%81%A1%E3%82%89%E3%81%AE%E3%82%B5% E3%82%A4%E3%83%88%E3%81%AB%E3%81%9F%E3%81%A9%E3%82%8A%E7%9D%80%E3%81%8D%E3% 81%BE%E3%81%97%E3%81%9F%E3%80%82

...还有HTML编码(& #nnn nn );, n = 0-9(?) )到:

  

ドイツ语で検索していてこちらのサイトにたどり着きました。

请帮助我,因为我现在完全失去了,甚至不知道从哪里开始。顺便说一下,现在表现对我来说并不重要。

提前致谢!

5 个答案:

答案 0 :(得分:3)

以下示例显示了两种方法,一种基于Qt库,另一种基于ICU库。两者都应该是平台无关的:

#include <iostream>
#include <sstream>
#include <iomanip>
#include <stdexcept>

#include <boost/scoped_array.hpp>

#include <QtCore/QString>
#include <QtCore/QUrl>
#include <QtCore/QVector>

#include <unicode/utypes.h>
#include <unicode/ustring.h>
#include <unicode/unistr.h>
#include <unicode/schriter.h>

void encodeQt() {
  const QString str = QString::fromWCharArray(L"ドイツ語で検索していてこちらのサイトにたどり着きました。");
  const QUrl url = str;
  std::cout << "URL encoded: " << url.toEncoded().constData() << std::endl;
  typedef QVector<uint> CodePointVector;
  const CodePointVector codePoints = str.toUcs4();
  std::stringstream htmlEncoded;
  for (CodePointVector::const_iterator it = codePoints.constBegin(); it != codePoints.constEnd(); ++it) {
    htmlEncoded << "&#" << *it << ';';
  }
  std::cout << "HTML encoded: " << htmlEncoded.str() << std::endl;
}

void encodeICU() {
  const std::wstring cppString = L"ドイツ語で検索していてこちらのサイトにたどり着きました。";
  int bufSize = cppString.length() * 2;
  boost::scoped_array<UChar> strBuffer(new UChar[bufSize]);
  int size = 0;
  UErrorCode error = U_ZERO_ERROR;
  u_strFromWCS(strBuffer.get(), bufSize, &size, cppString.data(), cppString.length(), &error);
  if (error) return;
  const UnicodeString str(strBuffer.get(), size);
  bufSize = str.length() * 4;
  boost::scoped_array<char> buffer(new char[bufSize]);
  u_strToUTF8(buffer.get(), bufSize, &size, str.getBuffer(), str.length(), &error);
  if (error) return;
  const std::string urlUtf8(buffer.get(), size);
  std::stringstream urlEncoded;
  urlEncoded << std::hex << std::setfill('0');
  for (std::string::const_iterator it = urlUtf8.begin(); it != urlUtf8.end(); ++it) {
    urlEncoded << '%' << std::setw(2) << static_cast<unsigned int>(static_cast<unsigned char>(*it));
  }
  std::cout << "URL encoded: " << urlEncoded.str() << std::endl;
  std::stringstream htmlEncoded;
  StringCharacterIterator it = str;
  while (it.hasNext()) {
    const UChar32 pt = it.next32PostInc();
    htmlEncoded << "&#" << pt << ';';
  }
  std::cout << "HTML encoded: " << htmlEncoded.str() << std::endl;
}


int main() {
  encodeQt();
  encodeICU();
}

答案 1 :(得分:1)

您可以看到,在将char转换为URL转义序列之前,必须将wstring *转换为用于URL的ISO-Latin字符集。 ICU可能是一个很好的起点,你可以将你的wstring传递给它并获得ISO-Lantin序列。然后,只需遍历生成的字符并将它们转换为转义序列:

std::stringstream URL;
URL << std::hex;
for(auto it = myWString.begin(); it != myWString.end(); ++it)
   URL << '%' << std::setfill('0') << std::setw(2) << (int)*it;

请查看here以获取有关如何格式化字符串的更多信息。

*我假设您的wstring是UTF-16,通常情况如此,但您没有指定

This也可能会有所帮助。

答案 2 :(得分:0)

这是一个使用Win32特定的WideCharToMultiByte()函数从UTF-16(wchar)转换为十六进制编码的UTF-8的版本。

#include <string>
#include <iostream>
#include <stdio.h>
#include <windows.h>


std::string wstring_to_utf8_hex(const std::wstring &input)
{
  std::string output;
  int cbNeeded = WideCharToMultiByte(CP_UTF8, 0, input.c_str(), -1, NULL, 0, NULL, NULL);
  if (cbNeeded > 0) {
    char *utf8 = new char[cbNeeded];
    if (WideCharToMultiByte(CP_UTF8, 0, input.c_str(), -1, utf8, cbNeeded, NULL, NULL) != 0) {
      for (char *p = utf8; *p; *p++) {
        char onehex[5];
        _snprintf(onehex, sizeof(onehex), "%%%02.2X", (unsigned char)*p);
        output.append(onehex);
      }
    }
    delete[] utf8;
  }
  return output;
}


int main(int, char*[])
{
  std::wstring ja = L"ドイツ語で検索していてこちらのサイトにたどり着きました。";
  std::cout << "result=" << wstring_to_utf8_hex(ja) << std::endl;
  return 0;
}

换句话说,你需要使用一些解析来将十六进制值解码为UTF-8缓冲区,然后调用补充的MultiByteToWideChar()将其恢复为wchar数组。

#include <string>
#include <iostream>
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>

std::string unhexlify(const std::string &input)
{
  std::string output;
  for (const char *p = input.c_str(); *p; ) {
    if (p[0] == '%' && isxdigit(p[1]) && isxdigit(p[2])) {
      int ch = (isdigit(p[1]) ? p[1] - '0' : toupper(p[1]) - 'A' + 10) * 16 + 
               (isdigit(p[2]) ? p[2] - '0' : toupper(p[2]) - 'A' + 10);
      output.push_back((char)ch);
      p += 3;
    } else if (p[0] == '%' && p[1] == '#' && isdigit(p[2])) {
      int ch = atoi(p + 2);
      output.push_back((char)ch);
      p += 2;
      while (*p && isdigit(*p)) p++;
      if (*p == ';') p++;
    } else {
      output.push_back(*p++);
    }
  }
  return output;
}


std::wstring utf8_hex_to_wstring(const std::string &input)
{
  std::wstring output;
  std::string utf8 = unhexlify(input);
  int cchNeeded = MultiByteToWideChar(CP_UTF8, 0, utf8.c_str(), -1, NULL, 0);
  if (cchNeeded > 0) {
    wchar_t *widebuf = new wchar_t[cchNeeded];
    if (MultiByteToWideChar(CP_UTF8, 0, utf8.c_str(), -1, widebuf, cchNeeded) != 0) {
      output = widebuf;
    }
    delete[] widebuf;
  }
  return output;
}

int main(int, char*[])
{
  std::wstring ja = L"ドイツ語で検索していてこちらのサイトにたどり着きました。";
  std::string hex = "%E3%83%89%E3%82%A4%E3%83%84%E8%AA%9E%E3%81%A7%E6%A4%9C%E7%B4%A2%E3%81%97%E3%81%A6%E3%81%84%E3%81%A6%E3%81%93%E3%81%A1%E3%82%89%E3%81%AE%E3%82%B5%E3%82%A4%E3%83%88%E3%81%AB%E3%81%9F%E3%81%A9%E3%82%8A%E7%9D%80%E3%81%8D%E3%81%BE%E3%81%97%E3%81%9F%E3%80%82";
  std::wstring newja = utf8_hex_to_wstring(hex);
  std::cout << "match?=" << (newja == ja ? "yes" : "no") << std::endl;
  return 0;
}

答案 3 :(得分:0)

首先,转换为UTF-8。 然后,正常的URL / HTML编码会做正确的事情。

答案 4 :(得分:0)

我发现在 C# 中很简单,所以我使用 C++\CLI 作为包装器,包装 C# 代码:

string encodedStr = System.Web.HttpUtility.UrlEncode(inputstr);`

C++\CLI 中创建一个方法为 __declspec(dllexport) 所以在 C++ 中可以调用它,C++\CLI 语法是:

String^ encodedStr  = System::Web::HttpUtility::UrlEncode(inputStr);`.

这是一个关于如何从 C++\CLI 调用 C++ 的教程:How to call a C# library from Native C++ (using C++\CLI and IJW)