fluffy/unhex.c

51 lines
871 B
C
Raw Normal View History

2013-01-29 21:53:17 -07:00
/*
* Hex Decoder -- 2012 Zephyr <zephyr@dirtbags.net> This file is in the public domain. I make no promises about the functionality
* of this program.
2012-02-10 11:29:56 -07:00
*/
#include <stdio.h>
int
main(int argc, char *argv[])
{
2013-01-29 21:53:17 -07:00
unsigned char acc = 0;
unsigned char nybble = 0;
unsigned long int count = 0;
2012-02-10 11:29:56 -07:00
2013-01-29 21:53:17 -07:00
while (1) {
int c = getchar();
2012-02-10 11:29:56 -07:00
2013-01-29 21:53:17 -07:00
count += 1;
2012-02-10 11:29:56 -07:00
2013-01-29 21:53:17 -07:00
switch (c) {
case EOF:
return 0;
case '0' ... '9':
acc = (acc << 4) + c - '0';
nybble += 1;
break;
case 'a' ... 'f':
acc = (acc << 4) + c - 'a' + 10;
nybble += 1;
break;
case 'A' ... 'F':
acc = (acc << 4) + c - 'A' + 10;
nybble += 1;
break;
default:
if (nybble != 0) {
fprintf(stderr, "Warning: non-hex character mid-octet at offset %lu\n", count);
}
break;
}
2012-02-10 11:29:56 -07:00
2013-01-29 21:53:17 -07:00
if (nybble == 2) {
putchar(acc);
acc = 0;
nybble = 0;
}
}
2012-02-10 11:29:56 -07:00
2013-01-29 21:53:17 -07:00
return 0;
2012-02-10 11:29:56 -07:00
}