csapp ch11.2 练习题

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_32768743/article/details/87518903

在这里插入图片描述
由于第1题完美解决了,这个问题就是变成了如何将十六进制字符串转换为整数
代码

#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) {
    int a;
    sscanf(argv[1], "%x", &a);
    ntop(a);
}

运行结果
在这里插入图片描述
答案
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_32768743/article/details/87518903