-1

This is in the context of interacting with /dev/dsp and pacat.

I would ideally like to be able to specify the encoding mechanism (big- versus little-endian, char/short/long).

As a bonus, some sort of packing of binary structures might be nice to have.

I'm basically thinking of something like numpy's dtype but at the shell.

Sample input / output

> seq 11 | bincat --big -n 1 | hd
01 02 03 04 05 06 07 08
09 0A 0B 
Att Righ
  • 1,196

1 Answers1

1

Programming languages should have no trouble with this task; here's one way to pack 64-bit integers (big or little endian) and native 32, 16, and 8-bit from a list of numbers in the input

$ cat input
-1
2
$ < input perl -pe '$_=pack "q>",$_' | xxd
00000000: ffff ffff ffff ffff 0000 0000 0000 0002  ................
$ < input perl -pe '$_=pack "q<",$_' | xxd
00000000: ffff ffff ffff ffff 0200 0000 0000 0000  ................
$ < input perl -pe '$_=pack "l",$_' | xxd
00000000: ffff ffff 0200 0000                      ........
$ < input perl -pe '$_=pack "s",$_' | xxd
00000000: ffff 0200                                ....
$ < input perl -pe '$_=pack "c",$_' | xxd
00000000: ff02                                     ..
$ 

The other way requires knowing what the binary has been written as as otherwise the numbers can come out all wrong

$ jot 255 | perl -pe '$_=pack "l>",$_' > 32big
$ < 32big perl -nE 'say for unpack "l*",$_' | head -3
16777216
33554432
50331648
$ < 32big perl -nE 'say for unpack "l>*",$_' | head -3
1
2
3
$ 
thrig
  • 34,938