#include <stdio.h>
#include <string.h>
//0xc0a82c05 转 192.168.44.5
int intToString(int num , char *buf ,int count)
{
int a = num/100;
int b = (num/10)%10;
int c = num%10;
if( a == 0)
{
if( b != 0)
{
buf[count++] = b + '0';
buf[count++] = c + '0';
}
else
{
buf[count++] = c + '0';
}
}
else
{
buf[count++] = a + '0';
buf[count++] = b + '0';
buf[count++] = c + '0';
}
return count;
}
char *intToChar(unsigned int number)
{
static char buf[20] = {0};
int count = 0;
int idx = 24;
while( idx >= 0 )
{
int num = (number>>idx) & 0xff;
count = intToString(num , buf , count);
if( idx != 0)
{
buf[count++] = '.';
}
idx -= 8;
}
return buf;
}
int main()
{
int number = 0xc0a82c05;
char *ret = intToChar( number );
printf("ret = %s \n", ret);
return 0;
}
C语言16进制转IP地址
于 2021-12-22 08:43:19 首次发布