Google
 

Tuesday, August 28, 2007

Convert 0x8002c2f2 as 128.2.194.242



#include  <stdlib.h>
#include 
< stdio.h>
#include 
<limits.h >
#include 
<string.h >

/*
    Convert  0x8002c2f2 as 128.2.194.242  
*/

int main( int argc, char ** argv)
{    
    
if(argc !=  2){
        fprintf(stderr,
"usage: %s <hex ip address> ",argv[0]);
        
return  0 ;
    }
    
    
char  *endp = 0
    
char * input  = argv[1];    
    unsigned 
long   hexAddr = strtoul(input,&endp, 16);    
    
    
if* endp != '\0 ')
    {
        printf(
"%s%s" ,"You have inputted is not Hex values:" ,input);
        
return 0;                                                                                                                                
    }
    
// checking overflow
    
// if( hexAddr == ULONG_MAX && *endp != '\0' )
//    {
//         printf("%s","Overflow exception! The max item is 0xFFFFFFFF");
//        return 0;                                                                                                                    
//    }  
 
    
int i;
    
int unit;
    
for (i=3; i >=  0 ; i--)
    {            
        unit 
= (hexAddr >>  8*i) &  0xff;
        
if(0  == i)
            printf(
"%d ", (int)unit);
        
else
            printf(
"%d.", ( int)unit);
    }
    
return 0  ;
}

--
Happy day, happy life!

No comments: