Browse Source

Added bd.pl

Alois Mahdal 11 years ago
parent
commit
fa67f15f24
2 changed files with 43 additions and 0 deletions
  1. 14
    0
      README.md
  2. 29
    0
      bin/bd.pl

+ 14
- 0
README.md View File

@@ -21,6 +21,20 @@ and around and be annoying about the fact that they fail.  Then the rule
21 21
 of thumb goes without saying: "no green, no commit".
22 22
 
23 23
 
24
+### bd.pl ###
25
+
26
+Binary dump.  Reads STDIN, 4 bytes at a time and displays it in similar way as
27
+`hexdump(1)` does with `-C` option (Canonical hex+ASCII display)
28
+
29
+Example:
30
+
31
+    $ echo "Hello world" | ./bd.pl
32
+    00000000  01001000 01100101 01101100 01101100  |Hell|
33
+    00000004  01101111 00100000 01110111 01101111  |o wo|
34
+    00000008  01110010 01101100 01100100 00001010  |rld.|
35
+    $
36
+
37
+
24 38
 ### mkexec.pl ###
25 39
 
26 40
 Make executable script for you and heat up vim.  Accepts filename as parameter.

+ 29
- 0
bin/bd.pl View File

@@ -0,0 +1,29 @@
1
+#!/usr/bin/perl
2
+use strict;
3
+use warnings;
4
+
5
+my $BUF_LENGTH = 4;
6
+my $ADR_LENGTH = 8;
7
+
8
+binmode STDIN;
9
+
10
+my $buff;
11
+my $addr = 0;
12
+while (read STDIN, $buff, $BUF_LENGTH) {
13
+    my $len = length $buff;
14
+    my @bytes = unpack "W" x $len, $buff;
15
+    my @b = map { sprintf "%08b", $_ } @bytes;
16
+    my $b = join " ",  @b;
17
+    my $h = join "", map {
18
+            $_ = 0x2e if $_ > 0x7f || $_ < 0x20;
19
+            sprintf "%c", $_
20
+        } @bytes;
21
+    my $bw = $BUF_LENGTH * 9 - 1;
22
+    printf
23
+        "%s  %s  |%s|\n",
24
+        sprintf ("%0${ADR_LENGTH}x", $addr),
25
+        sprintf ("%-${bw}s", $b),
26
+        sprintf ("%-${BUF_LENGTH}s", $h)
27
+    ;
28
+    $addr += $BUF_LENGTH;
29
+}