Hex dump: Difference between revisions

Content deleted Content added
Jgrprior (talk | contribs)
m Add links to task description.
PureFox (talk | contribs)
Added Wren
Line 433: Line 433:
00000060 00100000 00000000 00111101 11011000 00000000 11011110 | .=...|
00000060 00100000 00000000 00111101 11011000 00000000 11011110 | .=...|
00000066 00101110 00000000 |..|
00000066 00101110 00000000 |..|
00000068
</pre>

=={{header|Wren}}==
{{libheader|Wren-seq}}
{{libheader|Wren-fmt}}
This is only tested on Linux (Ubuntu 22.04).

As my text editor always adds a trailing \n, the method removes this before dumping the remainder.
<syntaxhighlight lang="ecmascript">import "io" for File
import "./seq" for Lst
import "./fmt" for Fmt

class DumpUtf16 {
static hex(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, 16)
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 {
ascii[b] = "."
}
}
var text = "|" + ascii.join() + (" " * (16-lc)) + "|"
if (i < rc-1) {
Fmt.print("$08x $02x $02x $s", i * 16, line[0..7], line[8..-1], text)
} else if (lc <= 8){
var extra = " " * (8-lc)
Fmt.print("$08x $02x$s $23s $s", i * 16, line[0..-1], extra, " ", text)
} else {
var extra = " " * (16-lc)
Fmt.print("$08x $02x $02x$s $s", i * 16, line[0..7], line[8..-1], 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 {
ascii[b] = "."
}
}
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", bytes.count)
}

static bin(fileName, start) { bin(fileName, start, -1) }
static bin(fileName) { bin(fileName, 0, -1) }
}

var fileName = "example_utf16.txt"
DumpUtf16.hex(fileName)
System.print()
DumpUtf16.bin(fileName)</syntaxhighlight>

{{out}}
<pre>
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.|
00000020 20 00 61 00 20 00 70 00 72 00 6f 00 67 00 72 00 | .a. .p.r.o.g.r.|
00000030 61 00 6d 00 6d 00 69 00 6e 00 67 00 20 00 63 00 |a.m.m.i.n.g. .c.|
00000040 68 00 72 00 65 00 73 00 74 00 6f 00 6d 00 61 00 |h.r.e.s.t.o.m.a.|
00000050 74 00 68 00 79 00 20 00 73 00 69 00 74 00 65 00 |t.h.y. .s.i.t.e.|
00000060 20 00 3d d8 00 de 2e 00 | .=..... |
00000068

00000000 11111111 11111110 01010010 00000000 01101111 00000000 |..R.o.|
00000006 01110011 00000000 01100101 00000000 01110100 00000000 |s.e.t.|
0000000c 01110100 00000000 01100001 00000000 00100000 00000000 |t.a. .|
00000012 01000011 00000000 01101111 00000000 01100100 00000000 |C.o.d.|
00000018 01100101 00000000 00100000 00000000 01101001 00000000 |e. .i.|
0000001e 01110011 00000000 00100000 00000000 01100001 00000000 |s. .a.|
00000024 00100000 00000000 01110000 00000000 01110010 00000000 | .p.r.|
0000002a 01101111 00000000 01100111 00000000 01110010 00000000 |o.g.r.|
00000030 01100001 00000000 01101101 00000000 01101101 00000000 |a.m.m.|
00000036 01101001 00000000 01101110 00000000 01100111 00000000 |i.n.g.|
0000003c 00100000 00000000 01100011 00000000 01101000 00000000 | .c.h.|
00000042 01110010 00000000 01100101 00000000 01110011 00000000 |r.e.s.|
00000048 01110100 00000000 01101111 00000000 01101101 00000000 |t.o.m.|
0000004e 01100001 00000000 01110100 00000000 01101000 00000000 |a.t.h.|
00000054 01111001 00000000 00100000 00000000 01110011 00000000 |y. .s.|
0000005a 01101001 00000000 01110100 00000000 01100101 00000000 |i.t.e.|
00000060 00100000 00000000 00111101 11011000 00000000 11011110 | .=...|
00000066 00101110 00000000 |.. |
00000068
00000068
</pre>
</pre>