等同于CLI中用于unicode编码的.net Uri.EscapeDataString

等同于CLI中用于unicode编码的.net Uri.EscapeDataString,.net,visual-c++,unicode,.net,Visual C++,Unicode,在.net中,我们有Uri.EscapeDataString,它为Unicode编码。它将“Ää”转换为“%C3%84%C3%A4”。CLI中的Uri.EscapeDataString的等效项是什么。我的代码是用VC++编写的,我不想使用Uri.EscapeDataString。我尝试了宽图表多字节(…)。但这并没有返回相同的结果。我可以在CLI中使用什么API,或者通过其他方式在CLI中获得相同的结果 在同事的帮助下,我终于得到了答案。首先使用WideChartMultiByte(..)将Wi

在.net中,我们有
Uri.EscapeDataString
,它为Unicode编码。它将“Ää”转换为“%C3%84%C3%A4”。CLI中的
Uri.EscapeDataString
的等效项是什么。我的代码是用VC++编写的,我不想使用Uri.EscapeDataString。我尝试了
宽图表多字节(…)
。但这并没有返回相同的结果。我可以在CLI中使用什么API,或者通过其他方式在CLI中获得相同的结果

在同事的帮助下,我终于得到了答案。首先使用WideChartMultiByte(..)将WideChart转换为multibye。i、 e将unicode转换为utf-8。然后一个字节一个字节地把它编码成十六进制

string methodTOHex()
{
  string s = "Ä";


  int len;
  int slength = (int)s.length() + 1;
  len = MultiByteToWideChar(CP_ACP, 0, s.c_str(), slength, 0, 0); 
  wchar_t* buf = new wchar_t[len];
  MultiByteToWideChar(CP_ACP, 0, s.c_str(), slength, buf, len);
  std::wstring temp(buf);
  delete[] buf;
  LPCWSTR input = temp.c_str();

  int cbNeeded = WideCharToMultiByte(CP_UTF8, 0, input, -1, NULL, 0, NULL, NULL);
  if (cbNeeded > 0) {
    char *utf8 = new char[cbNeeded];
    if (WideCharToMultiByte(CP_UTF8, 0, input, -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 += onehex;
      }
    }
    delete[] utf8;
  }

  return output;
}

我很清楚:如果您使用的是CLI,为什么不能直接使用.NET版本?您是否特别想要相同的非.NET版本?是的。。。我特别寻找CLI版本。我不应该在当前项目中使用.Net版本。可能您使用的“CLI”的含义与我想到的不同,因为当我听到“CLI”时,我想到的是“.Net VM”,因此不允许使用.Net实际上也不允许使用CLI。我在这里感到困惑吗?@MarcGravel:我的评论的意思是,我可以通过使用Uri.EscapeDataString(..)获得上面指定的输出。但它位于library System.dll下。但是我是用VC++编写代码的,我不应该在我的项目中使用System.dll。那么我还能用什么来得到同样的结果呢?看看这个问题