- 论坛徽章:
- 0
|
IP地址问题
try this:
x.x.x.x ->; xxxxxxxx
- #include <sys/socket.h>;
- #include <netinet/in.h>;
- #include <arpa/inet.h>;
- #include <stdio.h>;
- main(int argc, char *argv[])
- {
- char buffer[16];
- if (argc<2) {
- fgets(buffer,16,stdin);
- } else if (argc>;2) {
- fprintf(stderr,"Usage: %s xxx.xxx.xxx.xxx\n",argv[0]);
- } else {
- strncpy(buffer,argv[1],16);
- }
- printf("%u\n",ntohl(inet_addr(buffer)));
- }
复制代码
xxxxxxxx ->; x.x.x.x
- #include <stdio.h>;
- union convert {
- unsigned int i;
- unsigned char c[4];
- };
- main(int argc, char *argv[])
- {
- char buffer[16];
- union convert ip;
- if (argc<2) {
- fgets(buffer,16,stdin);
- } else if (argc>;2) {
- fprintf(stderr,"Usage: %s n\n",argv[0]);
- } else {
- strncpy(buffer,argv[1],16);
- }
- sscanf(buffer,"%u",&ip.i);
- printf("%d.%d.%d.%d\n",ip.c[3],ip.c[2],ip.c[1],ip.c[0]);
- }
复制代码 |
|