binview.pl 572 B

12345678910111213141516171819202122232425262728293031
  1. #!/usr/bin/perl
  2. # Author: Trizen
  3. # License: GPLv3
  4. # Date: 09 October 2013
  5. # https://trizenx.blogspot.com
  6. # Prints bits and bytes (or byte values) from a binary file.
  7. use 5.010;
  8. use strict;
  9. use autodie;
  10. use warnings;
  11. sub usage {
  12. print STDERR "usage: $0 file [cols]\n";
  13. exit 1;
  14. }
  15. my $file = shift() // usage();
  16. my $cols = shift() // 1;
  17. sysopen my $fh, $file, 0;
  18. while (sysread($fh, (my $chars), $cols) > 0) {
  19. foreach (split //, $chars) {
  20. printf "%10s%4s", unpack("B*"), /[[:print:]]/ ? $_ : sprintf("%03d", ord);
  21. }
  22. print "\n";
  23. }
  24. close $fh;