C语言字符串IP转4字节
点击查看代码
#include <stdio.h>
#include <string.h>
void str2ip(char *in, unsigned char *out)
{
int i;
int tmp[4]={0};
sscanf(in, "%d.%d.%d.%d", tmp, tmp + 1, tmp + 2, tmp + 3);
for (i = 0; i < 4; i++)
{
out[i] = tmp[i] & 0xff;
}
}
int main()
{
char *in = "192.168.1.1";
unsigned char out[4];
str2ip(in, out);
printf("\n%d.%d.%d.%d\n", out[0], out[1], out[2], out[3]);
}