inet_addr实现
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>// 方法1:使用指针数组实现
unsigned long inet_addr(const char *str)
{unsigned long lHost = 0;int i = 1, j = 1;const char *pstr[4] = { NULL };pstr[0] = strchr(str, '.');pstr[1] = strchr(pstr[0] + 1, '.');pstr[2] = strchr(pstr[1] + 1, '.');pstr[3] = strchr(str, '\0');for (j = 0; j < 4; j++){i = 1;if (j == 0){while (str != pstr[0]){lHost += (*--pstr[j] - '0') * i;i *= 10;}}else{while (*--pstr[j] != '.'){lHost += (*pstr[j] - '0') * i << 8 * j;i *= 10;}}}return lHost;
}// 方法2:使用strchr() + atoi()实现
unsigned long inet_addr2(const char *str)
{unsigned long lHost = 0;char *pLong = (char *)&lHost;char *p = (char *)str;while (p){*pLong++ = atoi(p);p = strchr(p, '.');if (p)++p;}return lHost;
}// 方法3:使用strtok()函数,对字符串进行切割
unsigned long inet_addr3(const char *str)
{unsigned long lHost = 0;char *pLong = (char *)&lHost;char substr[20] = { 0 };strncpy(substr, str, sizeof(substr));char *p = strtok(substr, ".");while (p != NULL){*pLong++ = atoi(p);p = strtok(NULL, "."); // 获取下一个片段}return lHost;
}// 方法4:使用联合体实现
union CUnion
{unsigned long lHost;unsigned short sHost[2];unsigned char cHost[4];
};unsigned inet_addr4(const char *str)
{CUnion u;int i = 0;char *p = (char *)str;while (p){u.cHost[i++] = atoi(p);p = strchr(p, '.');if (p)++p;}return u.lHost;
}
inet_addr实现相关推荐
- 解决gcc报错:error: implicit declaration of function ‘inet_addr’ [-Werror=implicit-function-declaration]
此文首发于我的个人博客:解决gcc报错 error implicit declaration of function 'inet_addr' [-Werror=implicit-function-de ...
- linux c socket ip地址 字符串 数字 转换 inet_addr inet_ntoa
目录 0.转换函数 1.介绍inet_addr函数 2.介绍inet_ntoa函数 3.一般使用总结 inet_addr 将字符串形式的IP地址 -> 网络字节顺序 的整型值 inet_nto ...
- 【Socket网络编程】3.字节序转换函数htons、htonl ,地址转换函数inet_ntoa、inet_ntop、inet_pton、inet_addr
字节序转换函数htons.htonl 地址转换函数inet_ntoa.inet_ntop.inet_pton.inet_addr 1.字节序转换函数 #include <arpa/inet.h& ...
- inet_aton和inet_network和inet_addr三者比较-《别怕Linux编程》之五
http://roclinux.cn/?p=1160 知识背景: 210.25.132.181属于IP地址的ASCII表示法,也就是字符串形式.英语叫做IPv4 numbers-and-dots no ...
- socket编程之inet_addr和inet_ntoa
inet_addr 将字符串形式的IP地址 -> 网络字节顺序 的整型值 inet_ntoa 网络字节顺序的整型值 ->字符串形式的IP地址 #define _WINSOCK_DEPRE ...
- linux网络编程之用一张图片说明函数inet_ntop、inet_pton、inet_addr、inet_ntoa 、inet_aton函数之间的关系
1.inet_ntop.inet_pton.inet_addr.inet_ntoa .inet_aton函数之间的关系 2.inet_ntop.inet_pton函数的源代码 1.inet_pton函 ...
- linux网络编程之inet_addr和inet_ntoa使用总结
1.介绍inet_addr函数 inet_addr函数转换网络主机地址(如192.168.1.10)为网络字节序二进制值,如果参数char *cp无效,函数返回-1(INADDR_NONE),这个函数 ...
- 【VS开发】IP地址格式转换(htonl、ntohl;inet_addr、inet_ntoa)
1.htonl ()和ntohl( ) u_long PASCAL FAR ntohl (u_long netlong); u_short PASCAL FAR ntohs (u_short nets ...
- IP地址转换函数——inet_pton inet_ntop inet_aton inet_addr inet_ntoa
inet_pton NAME inet_pton - 将 IPv4 和 IPv6 地址从点分十进制转换为二进制 SYNOPSIS #include <arpa/inet.h>int ...
- htonl() htons()及inet_ntoa() inet_addr()的用法
现在我们很幸运,因为我们有很多的函数来方便地操作 IP 地址.没有 必要用手工计算它们,也没有必要用"<<"操作来储存成长整字型. 首先,假设你已经有了一个sockad ...
最新文章
- 算法面试不懂技巧,你纯属被刷
- 【微软出品】AI-神经网络基本原理简明教程
- 智能睡眠评估系统(包含:睡眠数据特征工程、睡眠评分、睡眠聚类、睡眠可视化分析、睡眠结论存储)
- 列出所有已安装的perl模块
- mysql光标不见了_mysql创建光标失败
- php : RBAC 基于角色的用户权限控制-表参考
- 五十八、如何对一个数进行分解质因数
- c 结构体在声明时赋值_C/C++编程笔记:C++入门知识,C++多态性和虚函数解析
- MSSQL-最佳实践-Always Encrypted
- bert 多义词_自然语言处理:Bert及其他
- DBA跳槽应该去大公司还是小公司?99%的人这样说...
- Android开发 - ImageView加载Base64编码的图片
- android 7 apk 安装程序,Android安装apk文件并适配Android 7.0详解
- 【三电平SVPWM学习
- ESP8266 WIFI 模块和手机通信
- 2个令人震撼的Demo
- (论文阅读)实例分割之PolarMask
- json生成shp_shp与json互转(转载)
- opengles之展翅飞翔的雄鹰
- 新奥创:多APP场景端到端的技术体系探索与突破