Batch Convert Hex to Binary program?

Go To Last Post
9 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I have a file with hex values in this format

cc
cc
cc
fc
cc
cc
cc
00
ENDCHAR

78
30
30
30
30
30
78
00

I want to convert it to binary, I know how to do it one by one using an online translator but am looking to do all since there are 100+ chars.

Any program ?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Look at the library ultoa() and strtoul().

David.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

If you know C it would take about 15 minutes to write a C program to process this. Alternatively use an editor with macro recording facilities to simply add the punctuation to turn this into compilable C code.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I really need to learn how to make programs on the computer.

After going through the hell of manually changing from hex to binary, then formatting it by hand; adding 0b, commas and curvy brackets. Took a lot longer than I care to say :(

I ma surprised programs like these are not readily available. Only found a few online converters which change the format and does not even ignore commas or space.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Edit: Never mind, misread the OP.

Regards,
Steve A.

The Board helps those that help themselves.

Last Edited: Mon. Apr 1, 2013 - 04:02 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

If your hex was "standard" (ie just hex pairs) then a standard utility like xxd could convert it but it's the fact that it's a slightly unusual format that makes it more awkward.

As for doing it in C this is just sketchy as I'm typing on a tablet but:

 #include 
#include 

FILE * fin, * fout;
char line[80];

int main(int argc, char * argv[]) {
  fin = fopen(argv[1], "rt") ;
  fout = fopen("data.bin", "wb") ;
  do {
    fgets(line,  sizeof(line), fin) ;
    //process line[] 
    fputc (value,  fout) ;
  } while (! feof(fin)) ;
  fclose(fin) ;
  fclose(fout) ;
} 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

On your PC compile hexconsts.c:

#include 
#include 

void main(void)
{
	char buf[100], numbuf[20];
	long n;
	while (gets(buf) != NULL) {
		n = strtoul(buf, NULL, 16);
		putchar('0');
		putchar('b');
		puts(ltoa(n, numbuf, 2));
	}
}

run

hexconsts < inputhex.txt > outputbin.txt

I am sure that someone has written this before !

David.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

hexconsts.sed:

s/0/0000/g
s/1/0001/g
s/2/0010/g
s/3/0011/g
s/4/0100/g
s/5/0101/g
s/6/0110/g
s/7/0111/g
s/8/1000/g
s/9/1001/g
s/a/1010/g
s/b/1011/g
s/c/1100/g
s/d/1101/g
s/e/1110/g
s/f/1111/g
s/^[01]/0b&/

run

sed -f hexconsts.sed inputhex.txt

I like sed.

David.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Well, suppose you cannot program in C, but you have awk;
the file t.awk might countain something like that :

 # cat t.awk
BEGIN {print "char machaine[100]={" ;init=1;}
      {if (init !=1) printf(",");
      printf ("0x % s", $1); # the same trouble arrives with percent sign in awk and in C in this forum...
      init = init +1;
      }
END {print "};";}

An input file could look like that:
cat t.log
aO
Oc
aa

And, when you use awk whith this command and redirecting the input file like that

$ cat t.log | awk -f t.awk

youll get the following output :

char machaine[100]={
0xOO,0xOc,0xaa};

If you redirect it to an include file, it may be included in a c program.... and inside the c program, each item of "machaine " is binary... for a PC or an avr... (binaries are more likely for machines than for human beings..)

Of course, it might/should be refined (the string is always called machaine, one does not look whether hex numbers are real hex numbers, the generated string should be null terminated...)
The idea that awk (meant to teach C 40 yrs ago; now, interpreted c like pelles-c or picoc can be used for teaching) arrives more frequently than gcc on some unix/linux machines is a sad truth (I added gcc last week, telling gcc led to way faster softs..., though more resource greedy than awk)