Hex dump: Difference between revisions
Content deleted Content added
Added C solution |
→{{header|Wren}}: Rewritten to cater for more bases. |
||
Line 774: | Line 774: | ||
import "./fmt" for Fmt |
import "./fmt" for Fmt |
||
class |
class Dumper { |
||
static |
static dump(fileName, base, start, length) { |
||
if ([2, 3, 8, 10, 16].indexOf(base) == -1) Fiber.abort("Base %(base) is not supported.") |
|||
var bytes = File.read(fileName).bytes.toList[0..-3] // remove final \n |
|||
var bytes = File.read(fileName).bytes.toList |
|||
var bc = bytes.count |
var bc = bytes.count |
||
// remove final \n if present |
|||
if (start >= bc) Fiber.abort("Starting point is invalid.") |
|||
if (bc > 0 && bytes[-1] == 10) { |
|||
bytes.removeAt(-1) |
|||
bc = bc - 1 |
|||
} else if (bc > 1 && bytes[-1] == 0 && bytes[-2] == 10) { |
|||
bytes.removeAt(-1) |
|||
bytes.removeAt(-1) |
|||
bc = bc - 2 |
|||
} |
|||
if (bc == 0) Fmt.print("$q is empty.", fileName) |
|||
if (start < 0 || start >= bc) Fiber.abort("Starting point is invalid.") |
|||
if (length < 1 || length > bc - start) length = bc - start |
if (length < 1 || length > bc - start) length = bc - start |
||
var end = start + length - 1 |
|||
if (start > 0 || end < bc - 1) { |
|||
bytes = bytes[start..end] |
|||
bc = end - start + 1 |
|||
} |
|||
var rowLens = {2: 6, 3: 8, 8: 10, 10: 10, 16: 16} |
|||
var rl = rowLens[base] |
|||
var hrl = rl / 2 |
|||
var sp = (base < 8) ? " " : " " |
|||
var baseFmts = {2: "$08b", 3: "$06t", 8: "$03o", 10: "$03d", 16: "$02x"} |
|||
var bf = baseFmts[base] |
|||
var bl = Num.fromString(bf[2]) |
|||
var rows = Lst.chunks(bytes, rl) |
|||
var rc = rows.count |
var rc = rows.count |
||
Fmt.print("Dump of $q in base $d from byte index $d to $d:", fileName, base, start, end) |
|||
for (i in 0...rc) { |
for (i in 0...rc) { |
||
var line = rows[i] |
var line = rows[i] |
||
Line 795: | Line 818: | ||
} |
} |
||
} |
} |
||
var text = "|" + ascii.join() + (" " * ( |
var text = "|" + ascii.join() + (" " * (rl-lc)) + "|" |
||
var offset = start + i * rl |
|||
if (i < rc-1 || lc == rl) { |
|||
var fmt = "$08x %(bf)$s%(bf) $s" |
|||
Fmt.print(fmt, offset, line[0...hrl], sp, line[hrl..-1], text) |
|||
Fmt.print("$08x $02x$s $23s $s", i * 16, line[0..-1], extra, " ", text) |
|||
} else { |
} else { |
||
var extra = " |
var extra = " " * ((bl+1) * (rl - lc)) |
||
if (lc <= hrl) { |
|||
var fmt = "$08x %(bf)$s$s $s" |
|||
Fmt.print(fmt, offset, line[0..-1], sp, extra, text) |
|||
} |
|||
Fmt.print("$08x", bytes.count) |
|||
} |
|||
static hex(fileName, start) { hex(fileName, start, -1) } |
|||
static hex(fileName) { hex(fileName, 0, -1) } |
|||
static bin(fileName, start, length) { |
|||
var bytes = File.read(fileName).bytes.toList[0..-3] // remove final \n |
|||
var bc = bytes.count |
|||
if (start >= bc) Fiber.abort("Starting point is invalid.") |
|||
if (length < 1 || length > bc - start) length = bc - start |
|||
bytes = bytes[start...start+length] |
|||
var rows = Lst.chunks(bytes, 6) |
|||
var rc = rows.count |
|||
for (i in 0...rc) { |
|||
var line = rows[i] |
|||
var lc = line.count |
|||
var ascii = List.filled(lc, 0) |
|||
for (b in 0...lc) { |
|||
var c = line[b] |
|||
if (c >= 32 && c < 127) { |
|||
ascii[b] = String.fromByte(c) |
|||
} else { |
} else { |
||
var fmt = "$08x %(bf)$s%(bf)$s $s" |
|||
Fmt.print(fmt, offset, line[0...hrl], sp, line[hrl..-1], extra, text) |
|||
} |
} |
||
} |
|||
var text = "|" + ascii.join() + (" " * (6-lc)) + "|" |
|||
if (i < rc-1) { |
|||
Fmt.print("$08x $08b $s", i * 6, line, text) |
|||
} else { |
|||
var extra = " " * (6-lc) |
|||
Fmt.print("$08x $08b$s $s", i * 6, line, extra, text) |
|||
} |
} |
||
} |
} |
||
Fmt.print("$08x", |
Fmt.print("$08x", start + bc) |
||
} |
} |
||
static |
static dump(fileName, base, start) { dump(fileName, base, start, -1) } |
||
static |
static dump(fileName, base) { dump(fileName, base, 0, -1) } |
||
} |
} |
||
var fileName = "example_utf16.txt" |
var fileName = "example_utf16.txt" |
||
for (base in [16, 10, 8, 3, 2]) { |
|||
DumpUtf16.hex(fileName) |
|||
Dumper.dump(fileName, base) |
|||
System.print() |
|||
System.print() |
|||
DumpUtf16.bin(fileName)</syntaxhighlight> |
|||
} |
|||
Dumper.dump(fileName, 16, 2, 24)</syntaxhighlight> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Dump of "example_utf16.txt" in base 16 from byte index 0 to 103: |
|||
00000000 ff fe 52 00 6f 00 73 00 65 00 74 00 74 00 61 00 |..R.o.s.e.t.t.a.| |
00000000 ff fe 52 00 6f 00 73 00 65 00 74 00 74 00 61 00 |..R.o.s.e.t.t.a.| |
||
00000010 20 00 43 00 6f 00 64 00 65 00 20 00 69 00 73 00 | .C.o.d.e. .i.s.| |
00000010 20 00 43 00 6f 00 64 00 65 00 20 00 69 00 73 00 | .C.o.d.e. .i.s.| |
||
Line 863: | Line 860: | ||
00000068 |
00000068 |
||
Dump of "example_utf16.txt" in base 10 from byte index 0 to 103: |
|||
00000000 255 254 082 000 111 000 115 000 101 000 |..R.o.s.e.| |
|||
0000000a 116 000 116 000 097 000 032 000 067 000 |t.t.a. .C.| |
|||
00000014 111 000 100 000 101 000 032 000 105 000 |o.d.e. .i.| |
|||
0000001e 115 000 032 000 097 000 032 000 112 000 |s. .a. .p.| |
|||
00000028 114 000 111 000 103 000 114 000 097 000 |r.o.g.r.a.| |
|||
00000032 109 000 109 000 105 000 110 000 103 000 |m.m.i.n.g.| |
|||
0000003c 032 000 099 000 104 000 114 000 101 000 | .c.h.r.e.| |
|||
00000046 115 000 116 000 111 000 109 000 097 000 |s.t.o.m.a.| |
|||
00000050 116 000 104 000 121 000 032 000 115 000 |t.h.y. .s.| |
|||
0000005a 105 000 116 000 101 000 032 000 061 216 |i.t.e. .=.| |
|||
00000064 000 222 046 000 |.... | |
|||
00000068 |
|||
Dump of "example_utf16.txt" in base 8 from byte index 0 to 103: |
|||
00000000 377 376 122 000 157 000 163 000 145 000 |..R.o.s.e.| |
|||
0000000a 164 000 164 000 141 000 040 000 103 000 |t.t.a. .C.| |
|||
00000014 157 000 144 000 145 000 040 000 151 000 |o.d.e. .i.| |
|||
0000001e 163 000 040 000 141 000 040 000 160 000 |s. .a. .p.| |
|||
00000028 162 000 157 000 147 000 162 000 141 000 |r.o.g.r.a.| |
|||
00000032 155 000 155 000 151 000 156 000 147 000 |m.m.i.n.g.| |
|||
0000003c 040 000 143 000 150 000 162 000 145 000 | .c.h.r.e.| |
|||
00000046 163 000 164 000 157 000 155 000 141 000 |s.t.o.m.a.| |
|||
00000050 164 000 150 000 171 000 040 000 163 000 |t.h.y. .s.| |
|||
0000005a 151 000 164 000 145 000 040 000 075 330 |i.t.e. .=.| |
|||
00000064 000 336 056 000 |.... | |
|||
00000068 |
|||
Dump of "example_utf16.txt" in base 3 from byte index 0 to 103: |
|||
00000000 100110 100102 010001 000000 011010 000000 011021 000000 |..R.o.s.| |
|||
00000008 010202 000000 011022 000000 011022 000000 010121 000000 |e.t.t.a.| |
|||
00000010 001012 000000 002111 000000 011010 000000 010201 000000 | .C.o.d.| |
|||
00000018 010202 000000 001012 000000 010220 000000 011021 000000 |e. .i.s.| |
|||
00000020 001012 000000 010121 000000 001012 000000 011011 000000 | .a. .p.| |
|||
00000028 011020 000000 011010 000000 010211 000000 011020 000000 |r.o.g.r.| |
|||
00000030 010121 000000 011001 000000 011001 000000 010220 000000 |a.m.m.i.| |
|||
00000038 011002 000000 010211 000000 001012 000000 010200 000000 |n.g. .c.| |
|||
00000040 010212 000000 011020 000000 010202 000000 011021 000000 |h.r.e.s.| |
|||
00000048 011022 000000 011010 000000 011001 000000 010121 000000 |t.o.m.a.| |
|||
00000050 011022 000000 010212 000000 011111 000000 001012 000000 |t.h.y. .| |
|||
00000058 011021 000000 010220 000000 011022 000000 010202 000000 |s.i.t.e.| |
|||
00000060 001012 000000 002021 022000 000000 022020 001201 000000 | .=.....| |
|||
00000068 |
|||
Dump of "example_utf16.txt" in base 2 from byte index 0 to 103: |
|||
00000000 11111111 11111110 01010010 00000000 01101111 00000000 |..R.o.| |
00000000 11111111 11111110 01010010 00000000 01101111 00000000 |..R.o.| |
||
00000006 01110011 00000000 01100101 00000000 01110100 00000000 |s.e.t.| |
00000006 01110011 00000000 01100101 00000000 01110100 00000000 |s.e.t.| |
||
Line 882: | Line 924: | ||
00000066 00101110 00000000 |.. | |
00000066 00101110 00000000 |.. | |
||
00000068 |
00000068 |
||
Dump of "example_utf16.txt" in base 16 from byte index 2 to 25: |
|||
00000002 52 00 6f 00 73 00 65 00 74 00 74 00 61 00 20 00 |R.o.s.e.t.t.a. .| |
|||
00000012 43 00 6f 00 64 00 65 00 |C.o.d.e. | |
|||
0000001a |
|||
</pre> |
</pre> |