如题,本篇文章是为了测试IP地址转换函数 inet_aton 的实现过程,以及对二进制,八进制和十六进制 C++ 输出的测试,Ubuntu系统下,可通过 cat /usr/include/arpa/inet.h | grep inet_aton 查找原函数声明
下面提供测试程序及其结果:
#include <iostream>
#include <arpa/inet.h> // 网络编程中 包含 inet_aton函数,将点分十进制数串转换为32位网络字节序二进制值
#include <netinet/in.h> // 包含结构定义 struct in_addr{in_addr_t s_addr;}; 其中in_addr_t 为32位无符号整数 uint32_t
#include <iomanip> // hex 十六进制,oct 八进制 dec 十进制
#include <bitset>
using std::cout;
using std::endl;
using std::hex; // 16,10,8
using std::dec;
using std::oct;
using std::bitset;
int main(int argc, char ** argv)
{
char str[] = "192.168.229.200";
struct in_addr test;
cout<<"the IP str:"<<str<<endl;
int r = inet_aton(str,&test);
cout<<"the IP network byte binary value:"<<bitset<32>(test.s_addr)<<endl;
cout<<"the IP network byte dec value:"<<test.s_addr<<endl;
cout<<"the IP network byte hex value:"<<hex<<test.s_addr<<endl;
cout<<"the IP network byte oct value:"<<oct<<test.s_addr<<endl;
return 0;
}
结果:
the IP str:192.168.229.200
the IP network byte binary value:11001000111001011010100011000000
the IP network byte dec value:3370494144
the IP network byte hex value:c8e5a8c0
the IP network byte oct value:31071324300
1061




被折叠的 条评论
为什么被折叠?



