I wrote the following program to translate a hexstring to their corresponding binary data.
#include <stdio.h>
#include <stdlib.h>
int main(void) {
char bf[3];
char b; /* each byte */
bf[0] = bf[1] = bf[2] = 0;
for (;;) {
for (;;) {
bf[0] = getchar();
if (isspace(bf[0])) continue;
if (bf[0] == EOF) goto end;
break;
}
for (;;) {
bf[1] = getchar();
if (isspace(bf[1])) continue;
if (bf[1] == EOF) goto end;
break;
}
b = strtoul(bf, NULL, 16);
//printf("%s = %d\n", bf, b);
fwrite(&b, sizeof b, 1, stdout);
}
end:
exit(0);
}
Here's a test file:
%cat test.txt
E244050BF817B01D5E271F90052E0DD0
A9A5D1A2468E6908D4CF9951FC544A7B
0A5DF5692545A8856F3EF2CA5440A365
0FE4C9BC9854B042514E4805F0D0C4FF
Here's a run on a UNIX system (output perfectly as expected):
%./hex2bin < /mnt/test.txt | od -t x1
0000000 e2 44 05 0b f8 17 b0 1d 5e 27 1f 90 05 2e 0d d0
0000020 a9 a5 d1 a2 46 8e 69 08 d4 cf 99 51 fc 54 4a 7b
0000040 0a 5d f5 69 25 45 a8 85 6f 3e f2 ca 54 40 a3 65
0000060 0f e4 c9 bc 98 54 b0 42 51 4e 48 05 f0 d0 c4 ff
0000100
Here's a run on Windows system (a carriage return creeps in after byte 7b):
%./hex2bin.exe < test.txt | od -t x1
0000000 e2 44 05 0b f8 17 b0 1d 5e 27 1f 90 05 2e 0d d0
0000020 a9 a5 d1 a2 46 8e 69 08 d4 cf 99 51 fc 54 4a 7b
0000040 0d 0a 5d f5 69 25 45 a8 85 6f 3e f2 ca 54 40 a3
0000060 65 0f e4 c9 bc 98 54 b0 42 51 4e 48 05 f0 d0 c4
0000100 ff
0000101
%
The right sequence should be [...] 7b 0a [...] but it comes out as [...] 7b 0d 0a [...]. What's happening here?