Question

Trying to convert a hexstring given as string in the form 31ff097112 with plain awk into a binary file using cygwin (Works when run in linux). Tried the following:

BEGIN {
  BINMODE=3;
  ORS="";
  s="000102030405060708ff800f0e0d0c0b0a";
  # not working on cygwin for values >=0x80:
  len=length(s);
  print "">"broken.bin"
  for(i=1; i<=len; i+=2) {
    printf("%c", strtonum("0x"substr(s, i, 2)))>>"broken.bin";
  }
  #working, but uses external xxd command:
  print s>"/tmp/xxx";
  system ("cat /tmp/xxx|xxd -r -p>good.bin");
  exit;
}

So the problem with my plain awk solution is, that all values >= 0x80 are somehow written utf8-encoded to the resulting file:

$ hexdump -C broken.bin
00000000  00 01 02 03 04 05 06 07  08 c3 bf c2 80 0f 0e 0d  |................|
00000010  0c 0b 0a                                          |...|
00000013

whereas the xxd-tool (Found a hint there) does it right (as expected :-):

00000000  00 01 02 03 04 05 06 07  08 ff 80 0f 0e 0d 0c 0b  |................|
00000010  0a                                                |.|

So I would like to know wether there is a solution which works with awk (no, not perl, not python, not C) only using awk's features.

NB: My plain awk solutions works with linux awk perfect, so maybe there are some other options to be set within cygwin to make that work?

Was it helpful?

Solution

Sorry to answer my own question, just found the solution: invoke awk with -b or --characters-as-bytes, then it also works from within cygwin...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top