版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_32768743/article/details/87519257
代码
#include "csapp.h"
#include <arpa/inet.h>
void pton(const char * src) {
struct in_addr dst;
inet_pton(AF_INET, src, (void*)&dst);
printf("%s -> 0x%x\n", src, ntohl(dst.s_addr));
}
void ntop(const uint32_t hex) {
struct in_addr src;
src.s_addr = htonl(hex);
char dst[20];
inet_ntop(AF_INET, (void*)&src, dst, INET_ADDRSTRLEN);
printf("0x%x -> %s\n", src.s_addr, dst);
}
int main (int argc, char **argv) {
pton(argv[1]);
}
运行结果
答案