为了在使用URLDownloadToFile时 可以转换字符串以获取文件名而写的一个函数
已经通过了Hloli上的各种URL地址测试~
每次都是可以准确转换地~
网上找到的一些代码要不然就是转换不了中文要不然就用了string类
而本人不知道是为什么 异常讨厌string这东西……
So ,自己动手 丰衣足食~
多说无益,代码如下
//////////////////////////////////////////////////////////////////////////
// UTF-8 URL 地址解析 ANSI & UNICODE 代码
//作者:wye-MIA kid C!
//作者主页:http://wye-ANGer.blogspot.com
//转载请注明出处
#include <windows.h>
#include <stdio.h>
#define IsHexNum(c) ((c >= '0' && c <= '9') || (c >= 'A' && c <= 'F') || (c >= 'a' && c <= 'f'))
int HexToDec(const char* hex)
{
char buff[12];
sprintf(buff,"%s",hex);
int ret = 0;
int len = strlen(buff);
for(int i=0;i<len;i++)
{
char tmp[2];
tmp[0] = tolower(buff[i]);
tmp[1] = '\0';
int tmp_i;
if ('a' <= tmp[0] && tmp[0] <= 'g')
tmp_i = 10 - 'a' + tmp[0];
else tmp_i = tmp[0] - '0';
int rs = 1;
for(int j=i;j<(len-1);j++)
{
rs *= 16;
}
ret += (rs * tmp_i);
}
return ret;
}
CHAR *URLDecodeA(CHAR *URL_IN)
{
int len = strlen(URL_IN),pos=0;
char* URLA = new char[len + 1];
strcpy(URLA,URL_IN);
char* ret =new char[len + 1];
for(int i=0;i<len;i++)
{
if(URLA[i] == '%' && IsHexNum(URLA[i+1]) && IsHexNum(URLA[i+2]))
{
char tmp[2];
char hex[3];
hex[0] = URLA[++i];
hex[1] = URLA[++i];
hex[2] = '\0';
int hex_i = atoi(hex);
sprintf(tmp,"%c",HexToDec(hex));
strcat(ret,tmp);
}
else
{
ret[pos] = URLA[i];
ret[pos+1] = '\0';
}
pos++;
}
delete[] URLA;
ret[pos+1]='\0';
return ret;
}
WCHAR *URLDecodeW(WCHAR *URLW)
{
int len = wcslen(URLW),pos=0;
char* URLA = new char[len + 1];
WideCharToMultiByte(CP_ACP,0,URLW,len+1,URLA,len+1,NULL,NULL);
char* ret =new char[len + 1];
for(int i=0;i<len;i++)
{
if(URLA[i] == '%' && IsHexNum(URLA[i+1]) && IsHexNum(URLA[i+2]))
{
char tmp[2];
char hex[3];
hex[0] = URLA[++i];
hex[1] = URLA[++i];
hex[2] = '\0';
int hex_i = atoi(hex);
sprintf(tmp,"%c",HexToDec(hex));
strcat(ret,tmp);
}
else
{
ret[pos] = URLA[i];
ret[pos+1] = '\0';
}
pos++;
}
delete[] URLA;
ret[pos+1]='\0';
WCHAR *result = L"";
MultiByteToWideChar(CP_ACP,0,ret,pos+1,result,pos+1);
return result;
}
// UTF-8 URL 地址解析 ANSI & UNICODE 代码
//作者:wye-MIA kid C!
//作者主页:http://wye-ANGer.blogspot.com
//转载请注明出处
#include <windows.h>
#include <stdio.h>
#define IsHexNum(c) ((c >= '0' && c <= '9') || (c >= 'A' && c <= 'F') || (c >= 'a' && c <= 'f'))
int HexToDec(const char* hex)
{
char buff[12];
sprintf(buff,"%s",hex);
int ret = 0;
int len = strlen(buff);
for(int i=0;i<len;i++)
{
char tmp[2];
tmp[0] = tolower(buff[i]);
tmp[1] = '\0';
int tmp_i;
if ('a' <= tmp[0] && tmp[0] <= 'g')
tmp_i = 10 - 'a' + tmp[0];
else tmp_i = tmp[0] - '0';
int rs = 1;
for(int j=i;j<(len-1);j++)
{
rs *= 16;
}
ret += (rs * tmp_i);
}
return ret;
}
CHAR *URLDecodeA(CHAR *URL_IN)
{
int len = strlen(URL_IN),pos=0;
char* URLA = new char[len + 1];
strcpy(URLA,URL_IN);
char* ret =new char[len + 1];
for(int i=0;i<len;i++)
{
if(URLA[i] == '%' && IsHexNum(URLA[i+1]) && IsHexNum(URLA[i+2]))
{
char tmp[2];
char hex[3];
hex[0] = URLA[++i];
hex[1] = URLA[++i];
hex[2] = '\0';
int hex_i = atoi(hex);
sprintf(tmp,"%c",HexToDec(hex));
strcat(ret,tmp);
}
else
{
ret[pos] = URLA[i];
ret[pos+1] = '\0';
}
pos++;
}
delete[] URLA;
ret[pos+1]='\0';
return ret;
}
WCHAR *URLDecodeW(WCHAR *URLW)
{
int len = wcslen(URLW),pos=0;
char* URLA = new char[len + 1];
WideCharToMultiByte(CP_ACP,0,URLW,len+1,URLA,len+1,NULL,NULL);
char* ret =new char[len + 1];
for(int i=0;i<len;i++)
{
if(URLA[i] == '%' && IsHexNum(URLA[i+1]) && IsHexNum(URLA[i+2]))
{
char tmp[2];
char hex[3];
hex[0] = URLA[++i];
hex[1] = URLA[++i];
hex[2] = '\0';
int hex_i = atoi(hex);
sprintf(tmp,"%c",HexToDec(hex));
strcat(ret,tmp);
}
else
{
ret[pos] = URLA[i];
ret[pos+1] = '\0';
}
pos++;
}
delete[] URLA;
ret[pos+1]='\0';
WCHAR *result = L"";
MultiByteToWideChar(CP_ACP,0,ret,pos+1,result,pos+1);
return result;
}