ip 字符串 int

来源:互联网 发布:淘宝手机端有主图么 编辑:程序博客网 时间:2024/05/18 15:29


void DecodeHexChars(char *URL){
unsigned char *Source, *Dest;
int val, val2;



Source = strchr (URL, '%');


if (Source == NULL){
return;
}
Dest = Source;


while (*Source != 0){
if (*Source == '%'){
Source++;
val = *Source;


if (val > 'Z')
val -= 0x20;
val = val - '0';
if (val < 0)
val = 0;
if (val > 9)
val -= 7;
if (val > 15)
val = 15;


Source++;


val2 = *Source;


if (val2 > 'Z')
val2 -= 0x20;
val2 = val2 - '0';
if (val2 < 0)
val2 = 0;
if (val2 > 9)
val2 -= 7;
if (val2 > 15)
val2 = 15;
*Dest=val*16 + val2;
}else{
*Dest = *Source;
}
Dest++;
Source++;
}
*Dest = 0;
}




unsigned int inet_addr(const char *ip_addr){
        int   chs[4];
        typedef union   ip{
        unsigned   int   net_addr;
        unsigned   char   chs[4];
        }IP;
 
        IP   net_addr;
        sscanf(ip_addr,   "%d.%d.%d.%d",   &chs[0],   &chs[1],   &chs[2],   &chs[3]);
 
        net_addr.chs[3]   =   chs[3];
        net_addr.chs[2]   =   chs[2];
        net_addr.chs[1]   =   chs[1];
        net_addr.chs[0]   =   chs[0];
        return   net_addr.net_addr;
}


int char_to_int(char* s){
        int k = 0;
        while (*s)
        {
                k = 10 * k + (*s++)- '0';
 
        }
        return k;
}


void int_to_char(int m, int n, char *s){
    int p; 
int i1;
int mm;
        static int i;
        if(n==1){
        s[i++]=m%10+48;
                //s[i]='\0';
                return;
        }else{
        for(i1=0,mm=m,p=1;i1<n-1;i1++)
                {
                        p*=10;
                        mm/=10;
                }
 
                s[i++]=mm+48;
 
                int_to_char(m%p,n-1,s);
        }
}


char *inet_ntoa (__u32 ina){
static char buf[4*sizeof "123"];
unsigned char *ucp = (unsigned char *)&ina;
sprintf (buf, "%d.%d.%d.%d", ucp[0] & 0xff, ucp[1] & 0xff, ucp[2] & 0xff, ucp[3] & 0xff);
return buf;
}


int convert_words(char *convbuf, int len){
char *bp, *temp, *sp;
int found=0, elen=0;


temp=kmalloc(len, GFP_KERNEL);
if(temp==NULL){
printk("memory error\n");
return ERR;
}
memset(temp, 0x0, len);

bp=convbuf;
sp=temp;
while(*bp!='\0'){
if(*bp=='\\'){
bp++;
switch(*bp){
case 'n':
*sp='\n';
found=1;
break;
case 'r':
*sp='\r';
found=1;
break;
case 't':
*sp='\t';
found=1;
break;
default:
*sp='\\';
break;
}
if(found==1){
bp++;
elen++;
sp++;
continue;
}else{
elen++;
sp++;
continue;
}
}
*sp=*bp;
elen++;
sp++;
bp++;
continue;
}
temp[elen]='\0';
memcpy(convbuf, temp, elen);
convbuf[elen]='\0';
kfree(temp);
return OK;
}

原创粉丝点击