In .net we have Uri.EscapeDataString
which does encoding for unicodes. It converts "Ää" to "%C3%84%C3%A4". What is the equivalent for Uri.EscapeDataString
in CLI . My code is in VC++ and i don't want to use Uri.EscapeDataString. I tried with WideCharToMultiByte(...)
. But this is not returning same result. What is the API i can use in CLI or any other way is there to get the same results in CLI?
Asked
Active
Viewed 744 times
2

GAP
- 395
- 1
- 5
- 18
-
1just so I'm clear: if you are using the CLI, why can't you just use the .NET version? Are you specifically after a non-.NET version of the same? – Marc Gravell Mar 08 '13 at 12:07
-
Yes... i am specifically looking for CLI version. I am not supposed to use .Net version in the current project. – GAP Mar 08 '13 at 14:27
-
maybe you're using a different meaning of "CLI" than the one I'm thinking of, because when I hear "CLI" I'm thinking "the .NET VM", so disallowing .NET would also essentially disallow CLI. Am I confused here? – Marc Gravell Mar 08 '13 at 14:31
-
@MarcGravell: What i mean by my comment is , i can get the out put as specified above by using Uri.EscapeDataString(..). But it comes under the library System.dll. But i am coding in VC++ and i m not supposed to use the System.dll in my project. So what else i can use to get the same result? – GAP Mar 08 '13 at 16:18
-
1Look at this question http://stackoverflow.com/questions/154536/encode-decode-urls-in-c – polybios Mar 08 '13 at 16:22
-
CLI = Common Language Infrastructure. CLI = Command Line Interface. Avoid using "CLI" when asking questions about MSVC++. – Hans Passant Mar 08 '13 at 18:39
1 Answers
0
Finally i got the answer with the help of my colleague. first convert widechar to multibye using WideCharToMultiByte(..). i.e convert unicode to utf-8. then byte by byte we have to encode it to hex.
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;
}

GAP
- 395
- 1
- 5
- 18