csapp ch11.2 练习题
由于第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);
}
运行结果
答案