Skip to content

Instantly share code, notes, and snippets.

@rakusai
Created July 3, 2014 14:21
Show Gist options
  • Save rakusai/622f3b6137292797f7e7 to your computer and use it in GitHub Desktop.
Save rakusai/622f3b6137292797f7e7 to your computer and use it in GitHub Desktop.
C++でUTF8でURLエンコードされたコマンドライン引数を受け取る方法
ptrdiff_t urldecode(char* dst, const char* src)
{
char* org_dst = dst;
char ch, a, b;
do {
ch = *src++;
if (ch == '%' && isxdigit(a = src[0]) && isxdigit(b = src[1])) {
if (a < 'A') a -= '0';
else if(a < 'a') a -= 'A' - 10;
else a -= 'a' - 10;
if (b < 'A') b -= '0';
else if(b < 'a') b -= 'A' - 10;
else b -= 'a' - 10;
ch = 16 * a + b;
src += 2;
}
*dst++ = ch;
} while(ch);
return (dst - org_dst) - 1;
}
int urldecodeUnicode(WCHAR *original, int bufSize)
{
char *bufUTF8 = new char[bufSize];
char *bufDecodedUTF8 = new char[bufSize];
// Convert To UTF-8
WideCharToMultiByte(CP_UTF8, 0, original, -1, bufUTF8, bufSize, NULL, NULL);
// URL Decode
urldecode(bufDecodedUTF8, bufUTF8);
// Convert to Unicode
int r = MultiByteToWideChar(CP_UTF8, 0, bufDecodedUTF8, strlen(bufDecodedUTF8)+1, original, bufSize);
delete bufUTF8;
delete bufDecodedUTF8;
return r;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment