#include "stdio.h"
#include " stdlib.h"
#include <string .h>
/*
Convert 128.2.194.242 as 0x8002c2f2
*/
void ResetItem( char * item, int size)
{
// clear as \0
memset( item,0,size);
}
void SplitStr(int *unit, char* str, char splitter)
{
int i = 0; // item's index
int k = 0; // item char's index
char item[4];
ResetItem(item, 4);
do
{
if (splitter == *str || *str == '\0'){
// convert to int
unit[i] = atoi(item);
// increase item index
i++;
// reset item char's index
k = 0;
// clearn the char arry got
ResetItem(item,4 );
}else{
item[k] = *str;
k++;
}
if(*str != '\0' )
str++;
} while( i <4 );
}
int main( int argc, char ** argv)
{
// checing arguments
if(argc != 2){
fprintf(stderr,"usage: %s <ip dotted-decimal address> ",argv[0]);
return 0 ;
}
int unit[ 4];
SplitStr(unit,argv[1],' .');
printf(" %s","0x ");
int i;
for ( i = 0; i < 4;i++)
{
printf("%02x" ,unit[i]);
}
return 0 ;
}
#include " stdlib.h"
#include <string .h>
/*
Convert 128.2.194.242 as 0x8002c2f2
*/
void ResetItem( char * item, int size)
{
// clear as \0
memset( item,0,size);
}
void SplitStr(int *unit, char* str, char splitter)
{
int i = 0; // item's index
int k = 0; // item char's index
char item[4];
ResetItem(item, 4);
do
{
if (splitter == *str || *str == '\0'){
// convert to int
unit[i] = atoi(item);
// increase item index
i++;
// reset item char's index
k = 0;
// clearn the char arry got
ResetItem(item,4 );
}else{
item[k] = *str;
k++;
}
if(*str != '\0' )
str++;
} while( i <4 );
}
int main( int argc, char ** argv)
{
// checing arguments
if(argc != 2){
fprintf(stderr,"usage: %s <ip dotted-decimal address> ",argv[0]);
return 0 ;
}
int unit[ 4];
SplitStr(unit,argv[1],' .');
printf(" %s","0x ");
int i;
for ( i = 0; i < 4;i++)
{
printf("%02x" ,unit[i]);
}
return 0 ;
}
--
Happy day, happy life!
No comments:
Post a Comment