欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

C语言解析ipv4

程序员文章站 2024-03-12 22:12:14
...

// parseiP.cpp : 定义控制台应用程序的入口点。
//

include “stdafx.h”

include

include

include

pragma warning(disable:4996)

using namespace std;

define isip(a) ((a)>=0&&(a)<=255)

//返回n的二进制
void f(int n)
{
if (n)
f(n / 2);
else
return;
printf(“%d”, n % 2);
}

int main()
{
char *aa = “011”;

char dd = 5 + '0';
int cd = atoi(aa); 
printf("%c\n", dd);
system("pause");
int n = 5;
f(n);
map<int, char*>aa1;

pair<int, char*> cc = make_pair(2, const_cast<char*>("cc"));
aa1.insert(cc);
char *ip = "192.168.0.33";
int a=0, b=0, c=0, d=0;
char bip[32] = { 0 };
if (sscanf(ip, "%d.%d.%d.%d", &a, &b, &c, &d) == 4)
{
    if (isip(a) && isip(b) && isip(c) && isip(d))
    {
        printf("%d\n", a);
        itoa(a, bip, 2);
        f(a);
        printf("%s\n", bip);
        printf("%d\n", b);
        printf("%d\n", c);
        printf("%d\n", d);
        printf("%u", a << 24 | b << 16 | c << 8 | d);
        goto end;
    }
    else
        goto error;

}
else
    goto error;

error:
printf(“format error.”);
return -1;

end:
system(“pause”);
return 0;
}

相关标签: IPV4 parser