博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
字符串表示的IP地址与点分式表示的IP地址间的相互转化
阅读量:4048 次
发布时间:2019-05-25

本文共 939 字,大约阅读时间需要 3 分钟。

找了许久,找到了三个函数跟这个功能有关的。分别介绍一下把。

unsigned long inet_addr(  __in  const char *cp);
是把char *型的字符串数组的指针。

如char a[] = "192.168.2.1"  a为指针。转化为unsigned long型的。这个用处不大。

以下两个是主要的:

1)首先添加头文件#include <arpa/inet.h>

2)函数1:int inet_aton(const char * cp,struct in_addr *inp)

其中struct in_addr的结构体声明如下

struct in_addr{

unsigned long int s_addr;

}

这个函数是把字符串如"192.168.1.1"转化成这个结构体。

返回值的话 参看man吧。输入命令man inet_aton查看吧。

函数2:char *inet_ntoa(struct in_addr * inp)

把struct in_addr转化成char 数组。

举个栗子:

点分式char型数组转向unsigned int

struct in_addr _inaddrs,_inaddrd;   

struct tuple4 *ip_address;

ip_address = (struct tuple4 *)malloc(sizeof(struct tuple4 *));

memset(ip_address,0,sizeof(struct tuple4 *));

inet_aton("192.168.0.2", &_inaddrs);

ip_address->saddr = (unsigned int)_inaddrs.s_addr;

ip_address->saddr是u-int型的

转回来:unsigned int 转向点分式char型数组

struct in_addr _inaddrs,_inaddrd;

_inaddrs.s_addr = ip_address->saddr;

strcpy(a , inet_ntoa(_inaddrs));

总结 多利用msdn和man工具查询函数。多写一些函数、

转载地址:http://ptbci.baihongyu.com/

你可能感兴趣的文章
论文MICO for MRI bias field estimation and tissue segmentation品讲
查看>>
后现代
查看>>
VMware6关机后出现is not a valid virtual machine configuration file的解决办法
查看>>
通过ASP实现flash对数据库的访问
查看>>
“==”和equals方法究竟有什么区别?
查看>>
哈佛图书馆墙上的20条训言
查看>>
交流引发深入思考
查看>>
保持我们母语的纯洁
查看>>
免费的互联网时代如何盈利?
查看>>
可怕的宣传力量
查看>>
症状:可以上网,可以上QQ,不能登陆360安全卫士,360浏览器无法同步,有道词典等无法登陆,无法查询。
查看>>
重读《触龙说赵太后》
查看>>
2010的第一次思想触动
查看>>
文学大师做菜艺术20个"须知"
查看>>
SVN + 批处理 + Dropbox + TeamViewer实现远方协同工作
查看>>
vc学习之关于缩放到托盘区
查看>>
vc学习之键盘事件OnKeyDown
查看>>
近期工作
查看>>
春寒料峭,原来春天这样走近
查看>>
电脑坏了--关于联想笔记本声卡驱动
查看>>