-
URL Encode/DecodeProgramming/C,C++,C# 2007. 11. 20. 21:07반응형
URL 인코딩-----------------------------------------------------------------------
void UrlEncode(const char *inStr, char *outStr)
{
int inStrLen;
unsigned short asc;
unsigned char c;
int outIndex=0;
int i=0;
inStrLen = (int)strlen(inStr);
for(i=0; i<inStrLen; i++)
{
c = inStr[i];
asc = c;
if(asc>47 && asc<58 || asc>64 && asc<91 || asc>96 && asc<123)
outStr[outIndex++] = c;
else if(asc==32)
outStr[outIndex++] ='+';
else
{
char temp[4]={0,};
wsprintf(temp,"%%%2x",asc);
if((unsigned short)temp[1] == 32) temp[1]='0';
outStr[outIndex++] = temp[0];
outStr[outIndex++] = temp[1];
outStr[outIndex++] = temp[2];
}
}
outStr[outIndex]=0;
}
URL 디코딩-----------------------------------------------------------------------
void UrlDecode(const char *inStr, char *outStr)
{
int index = 0;
int num = 0;
int retval = 0;while(*inStr)
{
if(*inStr == '%')
{
int i = 0;
num = 0;
retval = 0;
for (int i = 0; i < 2; i++)
{
*inStr++;
if(*(inStr) < ':')
num = *(inStr) - 48;
else if(*(inStr) > '@' && *(inStr) < '[')
num = (*(inStr) - 'A')+10;
else
num = (*(inStr) - 'a')+10;
if((16*(1-i)))
num = (num*16);
retval += num;
}
outStr[index] = retval;
index++;
}
else
{
outStr[index] = *inStr;
index++;
}
*inStr++;
}
}반응형'Programming > C,C++,C#' 카테고리의 다른 글
strlen 함수를 만들어보자 (0) 2007.11.25 로또번호 생성기 (0) 2007.11.21 strlen 사용시 주의사항 (1) 2007.11.18