Created
July 3, 2014 14:21
-
-
Save rakusai/622f3b6137292797f7e7 to your computer and use it in GitHub Desktop.
C++でUTF8でURLエンコードされたコマンドライン引数を受け取る方法
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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