题解 | #整数与IP地址间的转换#
整数与IP地址间的转换
https://www.nowcoder.com/practice/66ca0e28f90c42a196afd78cc9c496ea
#include <stdio.h> typedef struct{ unsigned char n1; unsigned char n2; unsigned char n3; unsigned char n4; }IP_BYTE; typedef union{ unsigned int num; IP_BYTE ip_byte; }IP_DATA; int main() { int a1; int a2; int a3; int a4; IP_DATA ipA; IP_DATA ipB; scanf("%d.%d.%d.%d",&a1,&a2,&a3,&a4); ipA.ip_byte.n4 = a1; ipA.ip_byte.n3 = a2; ipA.ip_byte.n2 = a3; ipA.ip_byte.n1 = a4; printf("%ld\n",ipA.num); scanf("%d",&(ipB.num)); printf("%d.%d.%d.%d",ipB.ip_byte.n4,ipB.ip_byte.n3,ipB.ip_byte.n2,ipB.ip_byte.n1); return 0; }