C语言ip地址转十六进制

#include 
#include 
#include 

//192.168.44.5 转 0xc0a82c05
//0c  a8  2c 05

int toInt(const char *str)
{
	int num = 0;
	char buf[20] = {0};
	strcpy( buf , str );
	char *s = strtok( buf , ".");
	while( s != NULL )
	{
		num  = (num << 8) + atoi( s );
		s = strtok( NULL ,".");
	}
	return num;
}
int main()
{
	char *s = "192.168.44.5";
	int ret = toInt( s );
	printf("ret = %x\n" , ret);
	return 0;
}

你可能感兴趣的:(c语言)