#include
#include
#include
#include
#include
#define log_printf(msg...) fprintf(stderr, "hex2bin : "msg)
int inline hex2bin(char h4, char l4) {
if (h4 >= 'a') h4 = h4 - 'a' + 'A';
if (l4 >= 'a') l4 = l4 - 'a' + 'A';
if (h4 >= 'A') h4 = h4 - 'A' + 10;
else h4 = h4 - '0';
if (l4 >= 'A') l4 = l4 - 'A' + 10;
else l4 = l4 - '0';
return ((h4 << 4) | l4);
}
int main(int argc, char *argv[])
{
int fd;
char *name;
struct stat sb;
char *data;
int i;
if (argc != 2) {
log_printf("hex2bin filename!\nn");
return 1;
}
name = argv[1];
fd = open(name, O_RDWR);
if (fd < 0) {
log_printf("file %s is not exist\n", name);
return 1;
}
if (fstat(fd, &sb)) {
perror("fstat: ");
return 1;
}
data = mmap(NULL, sb.st_size, PROT_READ, MAP_SHARED, fd, 0);
if (data == MAP_FAILED) {
log_printf("mmap failed\n");
return 1;
}
for (i = 0; i < sb.st_size;) {
if ((data[i] == '\n') || (data[i] == '\r')) {
++i;
continue;
}
fprintf(stdout, "%c", hex2bin(data[i], data[i+1]));
i += 2;
}
}
下面test.hex为含有十六进制字符数据的文件,比如里面内容为:
140f0f1011b5223d79587717ffd9ec3a
af65bb470269ecd7af01f68f1a2b7b78
007ee80e7e04d27f4af09b4a55bbe5da
e38a6863169abfba3e78406f98c457f7
然后gliethttp.bin就是十六进制对应的二进制数据了,可以使用ghex2软件查空二进制数据[luther.gliethttp]
luther@gliethttp:/vobs/tmp$ gcc hex2bin.c
luther@gliethttp:/vobs/tmp$ ./a.out test.hex > gliethttp.bin
luther@gliethttp:/vobs/tmp$ ghex2 gliethttp.bin
阅读(1829) | 评论(0) | 转发(0) |