IP地址与点分十进制串的相互转换

概述

最近在学习Unix/Linux上的网络编程,遇到了IP地址与点分十进制串之间相互转换的问题,
所以写了两个程序用以实现它们之间的相互转换,以备不时之需.

十六进制参数转换为点分十进制串

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// a file named hexTodd.c 

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <arpa/inet.h>

#define MAXBUF 8192 /* Max I/O buffer size */

int main(int argc, char *argv[])
{
struct in_addr inaddr;
uint32_t addr;
long check;
char buf[MAXBUF];
int i = 0, m = 2;


if(argc != 2){
fprintf(stderr,"Usage: %s <hex number> for example: %s 0xfa256fd\n",argv[0], argv[0]);
exit(1);
}
if(argv[1][0] != '0' && (argv[1][1] != 'x' || argv[1][1] != 'X')){
fprintf(stderr,"Usage: %s <hex number> for example: %s 0xfa256fd\n",argv[0], argv[0]);
exit(1);
}
sscanf(argv[1],"%lx",&check);
if(check > 0xffffffff ){
fprintf(stderr,"The input max hex number should be 0xffffffff\n");
fprintf(stderr,"Usage: %s <hex number> for example: %s 0xfa256fd\n",argv[0], argv[0]);
exit(1);
}
while(argv[1][i] && argv[1][m]){
if(argv[1][0] == '0' && (argv[1][1] == 'x' || argv[1][1] == 'X' )){
if(isxdigit(argv[1][m])){
++m;
++i;
}else{
printf("In (0x)%s, %c is not a hex number!\n",argv[1],argv[1][m]);
fprintf(stderr,"Usage: %s <hex number> for example: %s 0xfa256fd\n",argv[0], argv[0]);
exit(1);
}
}else{
if(isxdigit(argv[1][i])){
++i;
}else{
printf("In (0x)%s, %c is not a hex number!\n",argv[1],argv[1][i]);
fprintf(stderr,"Usage: %s <hex number> for example: %s 0xfa256fd\n",argv[0], argv[0]);
exit(1);
}
}
}

sscanf(argv[1],"%x",&addr);
inaddr.s_addr = htonl(addr);

if(!inet_ntop(AF_INET, &inaddr, buf, MAXBUF)){
printf("inet_ntop\n");
}
else{
printf("%s\n",buf);
}

return 0;
}

用下面的命令编译并运行:

cc -g -o hexTodd hexTodd.c
./hexTodd

使用样例:

haha@hello:~/.../test-code$ ./hexTodd
Usage: ./hexTodd <(0x)hex number>
haha@hello:~/.../test-code$ ./hexTodd 8002c2f2
128.2.194.242
haha@hello:~/.../test-code$ ./hexTodd 0x8002c2f2
128.2.194.242
haha@hello:~/.../test-code$ ./hexTodd 0X8002c2f2
128.2.194.242
haha@hello:~/.../test-code$ ./hexTodd 8002c2f2x
In (0x)8002c2f2x, x is not a hex number!
Usage: ./hexTodd <(0x)hex number>
haha@hello:~/.../test-code$ ./hexTodd 8ac7dlpds
In (0x)8ac7dlpds, l is not a hex number!
Usage: ./hexTodd <(0x)hex number>
haha@hello:~/.../test-code$ 

点分十进制参数转换为十六进制数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// a file named ddTohex.c

#include <stdio.h>
#include <arpa/inet.h>

int main(int argc, char **argv)
{
struct in_addr inaddr; /* Address in network byte order */
int rc;

if (argc != 2) {
fprintf(stderr, "usage: %s <dotted-decimal>\n", argv[0]);
return 0;
}

rc = inet_pton(AF_INET, argv[1], &inaddr);
if (rc == 0)
printf("inet_pton error: invalid dotted-decimal address");
else if (rc < 0)
printf("inet_pton error");

printf("0x%x\n", ntohl(inaddr.s_addr));
return 0;
}

用下面的命令编译并运行:

cc -g -o ddTohex ddTohex.c
./ddTohex

使用样例:

haha@hello:~/.../test-code$ ./ddTohex
usage: ./ddTohex <dotted-decimal>
haha@hello:~/.../test-code$ ./ddTohex 128.2.194.242
0x8002c2f2
haha@hello:~/.../test-code$ ./ddTohex 1452.2253.2555.123
inet_pton error: invalid dotted-decimal address0xafffff
haha@hello:~/.../test-code$ ./ddTohex 126.125.252.8956
inet_pton error: invalid dotted-decimal address0xafffff
haha@hello:~/.../test-code$