Hex dump: Difference between revisions

23,355 bytes added ,  4 months ago
Add JavaScript
(Add JavaScript)
 
(27 intermediate revisions by 8 users not shown)
Line 13:
For example, the string "Rosetta Code is a programming chrestomathy site 😀." encoded in [[wp:UTF-16|UTF-16]] (little-endian - the first two bytes are the [[wp:Byte_order_mark|byte order mark]]), displayed in the canonical format is:
 
<syntaxhighlight lang="hexdump">
<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.|
Line 22:
00000060 20 00 3d d8 00 de 2e 00 | .=.....|
00000068
</syntaxhighlight>
</pre>
 
;Task
Line 40:
Implement a binary mode. For this task, in binary mode, the example above should be displayed like this:
 
<syntaxhighlight lang="hexdump">
<pre>
00000000 11111111 11111110 01010010 00000000 01101111 00000000 |..R.o.|
00000006 01110011 00000000 01100101 00000000 01110100 00000000 |s.e.t.|
Line 60:
00000066 00101110 00000000 |..|
00000068
</syntaxhighlight>
</pre>
 
Other hexdump/xxd features and a command line interface to your program are optional.
 
 
=={{header|C}}==
<syntaxhighlight lang="c">#include <ctype.h>
#include <errno.h>
#include <limits.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
 
#define BYTES_HEX 16
#define BYTES_BIN 6
 
static void print_hex(const char* data, long count) {
long i = 0;
for (; i < count; ++i) {
if (i % 8 == 0)
putchar(' ');
printf(" %02x", (unsigned char)data[i]);
}
for (; i < BYTES_HEX; ++i) {
if (i % 8 == 0)
putchar(' ');
printf(" ");
}
}
 
static void print_binary(const char* data, long count) {
long i = 0;
putchar(' ');
for (; i < count; ++i) {
unsigned char c = data[i];
putchar(' ');
for (unsigned char m = 1 << 7; m != 0; m >>= 1)
putchar((c & m) ? '1' : '0');
}
for (; i < BYTES_BIN; ++i)
printf(" ");
}
 
static void print_chars(const char* data, long count) {
for (long i = 0; i < count; ++i) {
unsigned char c = data[i];
putchar(isprint(c) ? c : '.');
}
}
 
static bool string_to_long(const char* str, long* value) {
char* eptr;
long result = strtol(str, &eptr, 0);
if (errno || *eptr || result < 0)
return false;
*value = result;
return true;
}
 
static void usage(const char* program) {
fprintf(stderr, "usage: %s [-b] [-s skip] [-n length] filename\n", program);
}
 
int main(int argc, char** argv) {
long offset = 0;
long max_length = LONG_MAX;
bool binary = false;
int i = 1;
for (; i < argc && argv[i][0] == '-'; ++i) {
switch (argv[i][1]) {
case 's':
if (++i == argc) {
usage(argv[0]);
return EXIT_FAILURE;
}
if (!string_to_long(argv[i], &offset)) {
fprintf(stderr, "Invalid skip '%s'.\n", argv[i]);
return EXIT_FAILURE;
}
break;
case 'n':
if (++i == argc) {
usage(argv[0]);
return EXIT_FAILURE;
}
if (!string_to_long(argv[i], &max_length)) {
fprintf(stderr, "Invalid length '%s'.\n", argv[i]);
return EXIT_FAILURE;
}
break;
case 'b':
binary = true;
break;
default:
usage(argv[0]);
return EXIT_FAILURE;
}
}
if (i + 1 != argc) {
usage(argv[0]);
return EXIT_FAILURE;
}
FILE* fp = fopen(argv[i], "rb");
if (!fp) {
perror(argv[i]);
return EXIT_FAILURE;
}
if (fseek(fp, 0, SEEK_END)) {
perror("fseek");
fclose(fp);
return EXIT_FAILURE;
}
long max_offset = ftell(fp);
if (offset > max_offset)
offset = max_offset;
if (fseek(fp, offset, SEEK_SET)) {
perror("fseek");
fclose(fp);
return EXIT_FAILURE;
}
char data[BYTES_HEX];
const long max_count = binary ? BYTES_BIN : BYTES_HEX;
for (long length = 0; length <= max_length;) {
printf("%08lx", offset);
long count = fread(data, 1, max_count, fp);
if (count > max_length - length)
count = max_length - length;
if (count == 0) {
putchar('\n');
break;
}
if (binary)
print_binary(data, count);
else
print_hex(data, count);
printf(" |");
print_chars(data, count);
printf("|\n");
offset += count;
length += count;
}
fclose(fp);
return EXIT_SUCCESS;
}</syntaxhighlight>
 
{{out}}
Hexadecimal output:
<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
</pre>
Binary output:
<pre>
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
</pre>
 
=={{header|FreeBASIC}}==
Line 237 ⟶ 412:
[https://jlaasonen.files.wordpress.com/2023/09/hexfile-1.3.1-windows-terminal-screenshot.png hexfile Windows terminal]
 
=={{header|PhixJ}}==
J's <code>hfd</code> (hex from decimal) is useful here, but it's not what the task asked for. We need to ensure a leading zero (achievable by adding 256 to the byte numbers, then dropping the resulting leading column of 1s), and then we need to massage the result to add spaces which fit the desired pattern.
<small>Aside: Phix does not really "do" utf-16, that is in source code terms, but declaring things as utf-8 and converting to utf-16 is not exactly hard.</small>
 
<!--<syntaxhighlight lang="phix">(phixonline)-->
The ascii section requires similar massaging, and some decorating.
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
 
<span style="color: #004080;">string</span> <span style="color: #000000;">utf8</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"\#EF\#BB\#BFRosetta Code is a programming chrestomathy site \xF0\x9F\x98\x80."</span>
The binary stretch goal could be approached similarly, but it's more concise to convert to 8 bit binary before massaging.
<span style="color: #004080;">sequence</span> <span style="color: #000000;">utf16</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">utf8_to_utf16</span><span style="color: #0000FF;">(</span><span style="color: #000000;">utf8</span><span style="color: #0000FF;">)</span>
 
<span style="color: #008080;">constant</span> <span style="color: #004080;">string</span> <span style="color: #000000;">s</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">peek</span><span style="color: #0000FF;">({</span><span style="color: #000000;">allocate_wstring</span><span style="color: #0000FF;">(</span><span style="color: #000000;">utf16</span><span style="color: #0000FF;">,</span><span style="color: #004600;">true</span><span style="color: #0000FF;">),</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">utf16</span><span style="color: #0000FF;">)*</span><span style="color: #000000;">2</span><span style="color: #0000FF;">})</span>
<syntaxhighlight lang=J>hexdump=: {{
<span style="color: #000080;font-style:italic;">--or:
assert. 'literal'-:datatype y
--constant string s = get_text(filename) -- (if you have a suitable premade one to hand)</span>
h=. '',~_2;\_8<@(' '(,,)])\' ',. 0 1}.hfd 256+a.i.y
a=. '',~_16(' |','|',~])\(y e.32}.127{.a.)}'.',:y
i=. 0 1}.hfd(2^32)+16*i.#h
i,.h,.a
}}
 
bindump=: {{
assert. 'literal'-:datatype y
b=. '',~_6(' ',,)\' ',.,/"2":"0(8#2)#:256+a.i.y
a=. '',~_6(' |','|',~])\(y e.32}.127{.a.)}'.',:y
i=. 0 1}.hfd(2^32)+6*i.#b
i,.b,.a
}}</syntaxhighlight>
 
J doesn't support a "little endian utf-16 literal" datatype, so we need to do a bit of work to create the required value.
 
<pre lang=J> sample=: a.{~_1 _2,,_1|."1]0 256#:3 u:7 u:'Rosetta Code is a programming chrestomathy site 😀.'
hexdump a.
00000000 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f |................|
00000010 10 11 12 13 14 15 16 17 18 19 1a 1b 1c 1d 1e 1f |................|
00000020 20 21 22 23 24 25 26 27 28 29 2a 2b 2c 2d 2e 2f | !"#$%&'()*+,-./|
00000030 30 31 32 33 34 35 36 37 38 39 3a 3b 3c 3d 3e 3f |0123456789:;<=>?|
00000040 40 41 42 43 44 45 46 47 48 49 4a 4b 4c 4d 4e 4f |@ABCDEFGHIJKLMNO|
00000050 50 51 52 53 54 55 56 57 58 59 5a 5b 5c 5d 5e 5f |PQRSTUVWXYZ[\]^_|
00000060 60 61 62 63 64 65 66 67 68 69 6a 6b 6c 6d 6e 6f |`abcdefghijklmno|
00000070 70 71 72 73 74 75 76 77 78 79 7a 7b 7c 7d 7e 7f |pqrstuvwxyz{|}~.|
00000080 80 81 82 83 84 85 86 87 88 89 8a 8b 8c 8d 8e 8f |................|
00000090 90 91 92 93 94 95 96 97 98 99 9a 9b 9c 9d 9e 9f |................|
000000a0 a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 aa ab ac ad ae af |................|
000000b0 b0 b1 b2 b3 b4 b5 b6 b7 b8 b9 ba bb bc bd be bf |................|
000000c0 c0 c1 c2 c3 c4 c5 c6 c7 c8 c9 ca cb cc cd ce cf |................|
000000d0 d0 d1 d2 d3 d4 d5 d6 d7 d8 d9 da db dc dd de df |................|
000000e0 e0 e1 e2 e3 e4 e5 e6 e7 e8 e9 ea eb ec ed ee ef |................|
000000f0 f0 f1 f2 f3 f4 f5 f6 f7 f8 f9 fa fb fc fd fe ff |................|
00000100
hexdump sample
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 | .=.....|
00000070
bindump sample
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 |..|
0000006c
</pre>
 
=={{header|JavaScript}}==
<syntaxhighlight lang="javascript">
// Generate UTF-16 LE bytes from a string.
function* utf16leBytes(str) {
// Little endian byte order mark.
yield 0xff;
yield 0xfe;
 
// Iterate code units.
let code_unit;
for (let i = 0; i < str.length; i++) {
code_unit = str.charCodeAt(i);
yield code_unit & 0xff;
yield code_unit >>> 8;
}
}
 
/**
* Generate sequences of length {@link n} from items in iterable {@link it}.
*/
function* groupBy(it, n) {
let chunk = [];
for (const item of it) {
chunk.push(item);
if (chunk.length == n) {
yield chunk;
chunk = [];
}
}
 
if (chunk.length) {
yield chunk;
}
}
 
/**
* @callback Formatter
* @param {Array<number>} chunk
* @returns {string} {@link chunk} formatted for output on one line.
*/
function canonicalFormatter(chunk) {
const bytesAsHex = chunk.map((byte) => byte.toString(16).padStart(2, "0"));
const hex = `${bytesAsHex.slice(0, 8).join(" ")} ${bytesAsHex
.slice(8)
.join(" ")}`.padEnd(48, " ");
 
const ascii = chunk
.map((byte) => (byte > 31 && byte < 127 ? String.fromCharCode(byte) : "."))
.join("");
 
return `${hex} |${ascii}|`;
}
 
/**
* @callback Formatter
* @param {Array<number>} chunk
* @returns {string} {@link chunk} formatted for output on one line.
*/
function binaryFormatter(chunk) {
const bits = chunk
.map((byte) => byte.toString(2).padStart(8, "0"))
.join(" ")
.padEnd(53, " ");
const ascii = chunk
<span style="color: #008080;">procedure</span> <span style="color: #000000;">hexdump</span><span style="color: #0000FF;">(</span><span style="color: #004080;">string</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">integer</span> <span style="color: #000000;">start</span><span style="color: #0000FF;">=</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">finish</span><span style="color: #0000FF;">=-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">bool</span> <span style="color: #000000;">bHex</span><span style="color: #0000FF;">=</span><span style="color: #004600;">true</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">bFinalAddr</span><span style="color: #0000FF;">=</span><span style="color: #004600;">true</span><span style="color: #0000FF;">)</span>
.map((byte) => (byte > 31 && byte < 127 ? String.fromCharCode(byte) : "."))
<span style="color: #004080;">integer</span> <span style="color: #000000;">ll</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">iff</span><span style="color: #0000FF;">(</span><span style="color: #000000;">bHex</span><span style="color: #0000FF;">?</span><span style="color: #000000;">16</span><span style="color: #0000FF;">:</span><span style="color: #000000;">6</span><span style="color: #0000FF;">)</span>
.join("");
<span style="color: #004080;">string</span> <span style="color: #000000;">hbfmt</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">iff</span><span style="color: #0000FF;">(</span><span style="color: #000000;">bHex</span><span style="color: #0000FF;">?</span><span style="color: #008000;">"%02x "</span><span style="color: #0000FF;">:</span><span style="color: #008000;">"%08b "</span><span style="color: #0000FF;">),</span>
 
<span style="color: #000000;">lnfmt</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"%08x %s |%-"</span> <span style="color: #0000FF;">&</span> <span style="color: #7060A8;">iff</span><span style="color: #0000FF;">(</span><span style="color: #000000;">bHex</span><span style="color: #0000FF;">?</span><span style="color: #008000;">"16"</span><span style="color: #0000FF;">:</span><span style="color: #008000;">"6"</span><span style="color: #0000FF;">)</span> <span style="color: #0000FF;">&</span> <span style="color: #008000;">"s|\n"</span>
return `${bits} |${ascii}|`;
<span style="color: #004080;">sequence</span> <span style="color: #000000;">lines</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">split_by</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">start</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">..</span><span style="color: #000000;">finish</span><span style="color: #0000FF;">],</span><span style="color: #000000;">ll</span><span style="color: #0000FF;">)</span>
}
<span style="color: #008080;">for</span> <span style="color: #000000;">l</span> <span style="color: #008080;">in</span> <span style="color: #000000;">lines</span> <span style="color: #008080;">do</span>
 
<span style="color: #004080;">string</span> <span style="color: #000000;">hb</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">""</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">ascii</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">""</span>
/**
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">,</span><span style="color: #000000;">b</span> <span style="color: #008080;">in</span> <span style="color: #000000;">l</span> <span style="color: #008080;">do</span>
* Generate a textual representation of bytes in {@link data} one line at a time.
<span style="color: #000000;">hb</span> <span style="color: #0000FF;">&=</span> <span style="color: #7060A8;">sprintf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">hbfmt</span><span style="color: #0000FF;">,</span><span style="color: #000000;">b</span><span style="color: #0000FF;">)</span>
*
<span style="color: #008080;">if</span> <span style="color: #000000;">b</span><span style="color: #0000FF;"><</span><span style="color: #008000;">' '</span> <span style="color: #008080;">or</span> <span style="color: #000000;">b</span><span style="color: #0000FF;">></span><span style="color: #008000;">'~'</span> <span style="color: #008080;">then</span> <span style="color: #000000;">b</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">'.'</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
* @param {Iterable<number>} data
<span style="color: #000000;">ascii</span> <span style="color: #0000FF;">&=</span> <span style="color: #000000;">b</span>
* @param {number} skip
<span style="color: #008080;">if</span> <span style="color: #7060A8;">mod</span><span style="color: #0000FF;">(</span><span style="color: #000000;">i</span><span style="color: #0000FF;">,</span><span style="color: #000000;">8</span><span style="color: #0000FF;">)=</span><span style="color: #000000;">0</span> <span style="color: #008080;">then</span> <span style="color: #000000;">hb</span> <span style="color: #0000FF;">&=</span> <span style="color: #008000;">' '</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
* @param {number} length
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
* @param {Formatter} formatter
<span style="color: #000000;">hb</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">pad_tail</span><span style="color: #0000FF;">(</span><span style="color: #000000;">hb</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">iff</span><span style="color: #0000FF;">(</span><span style="color: #000000;">bHex</span><span style="color: #0000FF;">?</span><span style="color: #000000;">51</span><span style="color: #0000FF;">:</span><span style="color: #000000;">54</span><span style="color: #0000FF;">))</span>
* @param {number} chunkSize
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">lnfmt</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">start</span><span style="color: #0000FF;">,</span><span style="color: #000000;">hb</span><span style="color: #0000FF;">,</span><span style="color: #000000;">ascii</span><span style="color: #0000FF;">})</span>
*/
<span style="color: #000000;">start</span> <span style="color: #0000FF;">+=</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">l</span><span style="color: #0000FF;">)</span>
function* hexDumpLines(
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
data,
<span style="color: #008080;">if</span> <span style="color: #000000;">bFinalAddr</span> <span style="color: #008080;">then</span>
skip = 0,
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%08x\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">start</span><span style="color: #0000FF;">})</span>
length = Infinity,
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
formatter = canonicalFormatter,
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
chunkSize = 16
) {
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Hexadecimal:\n"</span><span style="color: #0000FF;">)</span>
const it = data[Symbol.iterator]();
<span style="color: #000000;">hexdump</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">)</span>
 
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"\nBinary:\n"</span><span style="color: #0000FF;">)</span>
for (let i = 0; i < skip; i++) {
<span style="color: #000000;">hexdump</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">,</span><span style="color: #000000;">bHex</span><span style="color: #0000FF;">:=</span><span style="color: #004600;">false</span><span style="color: #0000FF;">)</span>
it.next();
<!--</syntaxhighlight>-->
}
 
let offset = skip;
let byteCount = 0;
let line = "";
 
for (let chunk of groupBy(data, chunkSize)) {
// Discard excess bytes if we've overshot length.
if (byteCount + chunk.length > length) {
chunk = chunk.slice(0, length - byteCount);
}
 
line = formatter(chunk);
yield `${offset.toString(16).padStart(8, "0")} ${line}`;
 
offset += chunkSize;
byteCount += chunk.length;
 
if (byteCount >= length) {
break;
}
}
 
// Final byte count
yield (byteCount + skip).toString(16).padStart(8, "0");
}
 
/**
* Log a hex dump of {@link data} to the console.
*
* @param {Iterable<number>} data
* @param {number} skip
* @param {number} length
* @param {Formatter} formatter
* @param {number} chunkSize
*/
function consoleHexDump(
data,
skip = 0,
length = Infinity,
formatter = canonicalFormatter,
chunkSize = 16
) {
for (const line of hexDumpLines(data, skip, length, formatter, chunkSize)) {
console.log(line);
}
}
 
const exampleData = "Rosetta Code is a programming chrestomathy site 😀.";
consoleHexDump(utf16leBytes(exampleData));
console.log("");
consoleHexDump(utf16leBytes(exampleData), 20, 25);
console.log("");
consoleHexDump(utf16leBytes(exampleData), 0, Infinity, binaryFormatter, 6);
</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
 
00000014 6f 00 64 00 65 00 20 00 69 00 73 00 20 00 61 00 |o.d.e. .i.s. .a.|
00000024 20 00 70 00 72 00 6f 00 67 | .p.r.o.g|
0000002d
 
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
</pre>
 
=={{header|Julia}}==
<syntaxhighlight lang="julia">function baseddump(io::IO, data::Vector{UInt8}; base = 16, offset = 0, len = -1, displayadjust = 0)
@assert 2 <= base <= 16 "display base $base not supported"
datastop = len < 0 ? length(data) : min(offset + len, length(data))
bytes = data[begin+offset:datastop]
fullchunksize = base == 16 ? 16 : base > 8 ? 10 : base > 4 ? 8 : 6
padsize = base == 16 ? 2 : base == 2 ? 8 : base > 7 ? 3 : base > 3 ? 4 : 5
midpad = " "^(base != 2)
vl = (padsize + 1) * fullchunksize + length(midpad)
halflen, pos = fullchunksize ÷ 2, offset + displayadjust
for chunk in Iterators.partition(bytes, fullchunksize)
chunklen = length(chunk)
values = map(n -> string(n, base = base, pad = padsize) * " ", chunk)
s1 = join(values[begin:begin+min(halflen, chunklen)-1])
if chunklen > halflen
s1 *= midpad * join(values[begin+halflen:end])
end
s2 = prod(map(n -> n < 128 && isprint(Char(n)) ? Char(n) : '.', chunk))
println(io, string(pos, base = 16, pad = 8) * " " * rpad(s1, vl) * "|" * s2 * "|")
pos += chunklen
end
println(io, string(pos - offset - displayadjust, base = 16, pad = 8))
end
function baseddump(data; base = 16, offset = 0, len = -1)
return baseddump(vec(collect(reinterpret(UInt8, data))); base, offset, len)
end
function baseddump(filename::AbstractString; base = 16, offset = 0, len = -1)
fromio = open(filename)
flen = stat(fromio).size
len = len < 0 ? flen - offset : min(len, flen - offset)
offset != 0 && seek(fromio, offset)
data::Vector{UInt8} = read(fromio, len)
baseddump(data; base = base, offset = 0, displayadjust = offset)
close(fromio)
end
hexdump(data; offset = 0, len = -1) = baseddump(data; offset, len)
xxd(data; offset = 0, len = -1) = baseddump(data; base = 2, offset, len)
decdump(data; offset = 0, len = -1) = baseddump(data; base = 10, offset, len)
 
const tstr = b"Rosetta Code is a programming chrestomathy site 😀."
const utf16 = vcat(b"\xff\xfe", reinterpret(UInt8, transcode(UInt16, tstr)))
print("hexdump of utf-16 string "), display(String(tstr))
hexdump(utf16)
print("\nxxd of utf-16 string "), display(String(tstr))
xxd(utf16)
print("\ndecdump of utf-16 string "), display(String(tstr))
decdump(utf16)
println("\nbaseddump of utf-16 string bytes 16-56 from file:")
fname = tempname()
open(fname, "w") do fd; write(fd, utf16) end
baseddump(fname, offset = 16, len = 40)
 
</syntaxhighlight>{{out}}
<pre>
hexdump of utf-16 string "Rosetta Code is a programming chrestomathy site 😀."
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
 
xxd of utf-16 string "Rosetta Code is a programming chrestomathy site 😀."
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
 
decdump of utf-16 string "Rosetta Code is a programming chrestomathy site 😀."
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
 
baseddump of utf-16 string bytes 16-56 from file:
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 |a.m.m.i.|
00000028
</pre>
 
=={{header|Phix}}==
<!--(phixonline)-->
<syntaxhighlight lang="phix">
with javascript_semantics
sequence utf16 = #FEFF & "Rosetta Code is a programming chrestomathy site " & {#d83d,#DE00,'.'}
function to2(integer i) return {and_bits(i,#FF),floor(i/#100)} end function
constant string s = flatten(apply(utf16,to2),"")
--or: (javascript incompatible)
--constant string s = get_text(filename) -- (if you have a suitable premade one to hand)
 
procedure hexdump(string s, integer start=0, finish=-1, bool bHex=true, bFinalSize=true)
s = s[start+1..finish]
integer ll = iff(bHex?16:6), ls = length(s)
string hbfmt = iff(bHex?"%02x ":"%08b "),
lnfmt = "%08x %s |%-" & sprintf("%d",ll) & "s|\n"
sequence lines = split_by(s,ll)
for l in lines do
string hb = " ", ascii = ""
for i,b in l do
hb &= sprintf(hbfmt,b)
if b<' ' or b>'~' then b = '.' end if
ascii &= b
if i=8 then hb &= ' ' end if
end for
hb = pad_tail(hb,iff(bHex?50:55))
printf(1,lnfmt,{start,hb,ascii})
start += length(l)
end for
if bFinalSize then
printf(1,"%08x\n",{ls})
end if
end procedure
 
printf(1,"Hexadecimal:\n")
hexdump(s)
printf(1,"\nBinary:\n")
hexdump(s,bHex:=false)
</syntaxhighlight>
Although not shown, the intention is the outer calling code (last four lines) could call hexdump() to display one page at a time, with appropriate user input, and/or read s in chunks and pass a file offset instead of or as well as start, finish.
{{out}}
<pre>
Hexadecimal:
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
 
Binary:
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
</pre>
 
=={{header|Python}}==
<syntaxhighlight lang="python">
Line 313 ⟶ 855:
import math
from io import BufferedIOBase
from io import SEEK_END
from itertools import islice
from typing import Iterable
Line 328 ⟶ 871:
 
@abc.abstractmethod
def __call__format(self, data: Sequence[int]) -> str:
"""Return _data_ formatted for output on one line."""
 
@property
@abc.abstractmethod
def bytes_per_line(self) -> int:
"""The number of bytes to display per line of output."""
 
 
Line 340 ⟶ 883:
bytes_per_line = 16
 
def __call__format(self, data: Sequence[int]) -> str:
assert len(data) <= 16
hex = f"{bytes(data[:8]).hex(' ')} {bytes(data[8:]).hex(' ')}".ljust(48)
Line 350 ⟶ 893:
bytes_per_line = 6
 
def __call__format(self, data: Sequence[int]) -> str:
assert len(data) <= 6
bits = " ".join(bin(b)[2:].rjust(8, "0") for b in data).ljust(53)
Line 381 ⟶ 924:
skip: int = 0,
length: int = math.inf, # type: ignore
formatformatter: Formatter = canonicalFormatter,
) -> Iterator[str]:
"""Generate a textual representation of bytes in _f_, one line at a time."""
f.seek(skip)
offset = 0skip
byte_count = 0
previous_line = ""
squeezing = False # True if the last output line was squeezed
identical_chunk = False
 
assert length > 0
 
while byte_count < length:
Line 403 ⟶ 948:
 
# One line per chunk
for chunk in group(data, formatformatter.bytes_per_line):
line = formatter.format(chunk)
if previous_line == line:
if identical_chunksqueezing is False:
identical_chunksqueezing = True
yield "*"
else:
previous_line = line
identical_chunksqueezing = False
yield f"{offset:0>8x} {line}"
 
offset += formatformatter.bytes_per_line
byte_count += len(chunk)
 
# Final byte count
bytes_in_f = f.seek(0, SEEK_END)
yield f"{byte_count:0>8x}"
yield f"{min(byte_count + skip, bytes_in_f):0>8x}"
 
 
Line 464 ⟶ 1,010:
for line in hex_dump(
args.file,
formatformatter=formatter,
skip=args.skip,
length=args.length,
Line 482 ⟶ 1,028:
00000060 20 00 3d d8 00 de 2e 00 | .=.....|
00000068
</pre>
<pre>$ python hex_dump.py example_utf16.txt -s 20 -n 25</pre>
<pre>
00000014 6f 00 64 00 65 00 20 00 69 00 73 00 20 00 61 00 |o.d.e. .i.s. .a.|
00000024 20 00 70 00 72 00 6f 00 67 | .p.r.o.g|
0000002d
</pre>
<pre>$ python hex_dump.py example_utf16.txt -b</pre>
Line 505 ⟶ 1,057:
00000068
</pre>
 
=={{header|Raku}}==
The hexdump() routine expects a Blob. How you feed it the Blob is up to you. You may read a file in binary mode, encode a string, whatever. Using encoded strings here for a compact, self-contained example. Encoded in a few different ways for variety.
 
<syntaxhighlight lang="raku" line>say my $string = 'Rosettacode is a programming crestomathy site 😀. 🍨 👨‍👩‍👦 ⚽ ¯\_(ツ)_/¯';
 
say "\nHex dump UTF-16BE, offset 0";
hexdump $string.encode("UTF-16BE");
 
say "\nHex dump UTF-16LE, offset 0";
hexdump $string.encode("UTF-16LE");
 
say "\nBinary dump UTF-8, offset 17";
hexdump $string.encode("UTF-8"), :bin, :17offset;
 
sub hexdump(Blob $blob, Int :$offset is copy = 0, :$bin = False) {
my ($fmt, $space, $batch) = $bin ?? ("%08b", ' ' x 8, 6) !! ("%02X", ' ', 16);
for $blob.skip($offset).batch($batch) {
my @h = flat $_».fmt($fmt), $space xx $batch;
@h[7] ~= ' ';
printf "%08X %s |%s|\n", $offset, @h[^$batch].Str,
$_».chr.join.subst(/<-print>|\t|\n/, '.', :g);
$offset += $batch;
}
}</syntaxhighlight>
{{out}}
<pre>Rosettacode is a programming crestomathy site 😀. 🍨 👨‍👩‍👦 ⚽ ¯\_(ツ)_/¯
 
Hex dump UTF-16BE, offset 0
00000000 00 52 00 6F 00 73 00 65 00 74 00 74 00 61 00 63 |.R.o.s.e.t.t.a.c|
00000010 00 6F 00 64 00 65 00 20 00 69 00 73 00 20 00 61 |.o.d.e. .i.s. .a|
00000020 00 20 00 70 00 72 00 6F 00 67 00 72 00 61 00 6D |. .p.r.o.g.r.a.m|
00000030 00 6D 00 69 00 6E 00 67 00 20 00 63 00 72 00 65 |.m.i.n.g. .c.r.e|
00000040 00 73 00 74 00 6F 00 6D 00 61 00 74 00 68 00 79 |.s.t.o.m.a.t.h.y|
00000050 00 20 00 73 00 69 00 74 00 65 00 20 D8 3D DE 00 |. .s.i.t.e. Ø=Þ.|
00000060 00 2E 00 20 00 20 00 20 00 20 D8 3C DF 68 00 20 |... . . . Ø<ßh. |
00000070 D8 3D DC 68 20 0D D8 3D DC 69 20 0D D8 3D DC 66 |Ø=Üh .Ø=Üi .Ø=Üf|
00000080 00 20 26 BD 00 20 00 AF 00 5C 00 5F 00 28 30 C4 |. &½. .¯.\._.(0Ä|
00000090 00 29 00 5F 00 2F 00 AF |.)._./.¯|
 
Hex dump UTF-16LE, offset 0
00000000 52 00 6F 00 73 00 65 00 74 00 74 00 61 00 63 00 |R.o.s.e.t.t.a.c.|
00000010 6F 00 64 00 65 00 20 00 69 00 73 00 20 00 61 00 |o.d.e. .i.s. .a.|
00000020 20 00 70 00 72 00 6F 00 67 00 72 00 61 00 6D 00 | .p.r.o.g.r.a.m.|
00000030 6D 00 69 00 6E 00 67 00 20 00 63 00 72 00 65 00 |m.i.n.g. .c.r.e.|
00000040 73 00 74 00 6F 00 6D 00 61 00 74 00 68 00 79 00 |s.t.o.m.a.t.h.y.|
00000050 20 00 73 00 69 00 74 00 65 00 20 00 3D D8 00 DE | .s.i.t.e. .=Ø.Þ|
00000060 2E 00 20 00 20 00 20 00 20 00 3C D8 68 DF 20 00 |.. . . . .<Øhß .|
00000070 3D D8 68 DC 0D 20 3D D8 69 DC 0D 20 3D D8 66 DC |=ØhÜ. =ØiÜ. =ØfÜ|
00000080 20 00 BD 26 20 00 AF 00 5C 00 5F 00 28 00 C4 30 | .½& .¯.\._.(.Ä0|
00000090 29 00 5F 00 2F 00 AF 00 |)._./.¯.|
 
Binary dump UTF-8, offset 17
00000011 01110000 01110010 01101111 01100111 01110010 01100001 |progra|
00000017 01101101 01101101 01101001 01101110 01100111 00100000 |mming |
0000001D 01100011 01110010 01100101 01110011 01110100 01101111 |cresto|
00000023 01101101 01100001 01110100 01101000 01111001 00100000 |mathy |
00000029 01110011 01101001 01110100 01100101 00100000 11110000 |site ð|
0000002F 10011111 10011000 10000000 00101110 00100000 00100000 |.... |
00000035 00100000 00100000 11110000 10011111 10001101 10101000 | ð..¨|
0000003B 00100000 11110000 10011111 10010001 10101000 11100010 | ð..¨â|
00000041 10000000 10001101 11110000 10011111 10010001 10101001 |..ð..©|
00000047 11100010 10000000 10001101 11110000 10011111 10010001 |â..ð..|
0000004D 10100110 00100000 11100010 10011010 10111101 00100000 |¦ â.½ |
00000053 11000010 10101111 01011100 01011111 00101000 11100011 |¯\_(ã|
00000059 10000011 10000100 00101001 01011111 00101111 11000010 |..)_/Â|
0000005F 10101111 |¯|</pre>
 
=={{header|Wren}}==
Line 512 ⟶ 1,131:
 
As my text editor always adds a trailing \n, the method removes this before dumping the remainder.
<syntaxhighlight lang="ecmascriptwren">import "io" for File
import "./seq" for Lst
import "./fmt" for Fmt
 
class DumpUtf16Dumper {
static hexdump(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
// 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
bytesvar end = bytes[start...start + length] - 1
varif rows(start => Lst.chunks(bytes,0 16|| 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
Fmt.print("Dump of $q in base $d from byte index $d to $d:", fileName, base, start, end)
for (i in 0...rc) {
var line = rows[i]
Line 537 ⟶ 1,179:
}
}
var text = "|" + ascii.join() + (" " * (16-lc)) + "|"
ifvar (ioffset <= start + i rc-1)* {rl
if Fmt.print("$08xi < $02xrc-1 || $02xlc == $s",rl) i * 16, line[0..7], line[8..-1], text){
} else if (lc <var fmt = 8"$08x %(bf){$s%(bf) $s"
varFmt.print(fmt, extraoffset, =line[0...hrl], "sp, line[hrl..-1], " * (8-lctext)
Fmt.print("$08x $02x$s $23s $s", i * 16, line[0..-1], extra, " ", text)
} else {
var extra = " " * "((bl+1) * (16rl - lc))
Fmt.printif ("$08xlc <= $02xhrl) $02x$s $s", i * 16, line[0..7], line[8..-1], extra, text){
} 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 {
ascii[b]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", bytes.countstart + bc)
}
 
static bindump(fileName, base, start) { bindump(fileName, base, start, -1) }
static bindump(fileName, base) { bindump(fileName, base, 0, -1) }
}
 
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}}
<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.|
00000010 20 00 43 00 6f 00 64 00 65 00 20 00 69 00 73 00 | .C.o.d.e. .i.s.|
Line 602 ⟶ 1,218:
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
 
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.|
00000006 01110011 00000000 01100101 00000000 01110100 00000000 |s.e.t.|
Line 622 ⟶ 1,283:
0000005a 01101001 00000000 01110100 00000000 01100101 00000000 |i.t.e.|
00000060 00100000 00000000 00111101 11011000 00000000 11011110 | .=...|
00000066 00101110 00000000 |.. |
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>
 
=={{header|XPL0}}==
Works for both MS-DOS and RPi Linux.
<syntaxhighlight lang "XPL0">int Offset, Length, Limit, Byte;
char FileName(80);
 
func GetByte; \Return a byte from input file
int Return;
[Return:= Byte;
Byte:= ChIn(3); \one-byte look-ahead detects EOF
return Return;
];
 
proc DumpHex(I); \Show line of hex and its ASCII, starting at offset I
int I, J;
char Line(16);
[HexOut(0, I); Text(0, " ");
SetHexDigits(2);
for J:= 0 to 15 do
[Line(J):= GetByte;
if GetErr and I+J < Limit then Limit:= I+J;
if I+J < Limit then
HexOut(0, Line(J))
else Text(0, " ");
ChOut(0, ^ );
if (J&7) = 7 then ChOut(0, ^ );
];
SetHexDigits(8); \restore default number of digits
ChOut(0, ^|); \display corresponding ASCII characters
for J:= 0 to 15 do
if I+J < Limit then
ChOut(0, if Line(J)<=$1F or Line(J)>=$7F then ^. else Line(J));
ChOut(0, ^|); CrLf(0);
];
 
func OpenInFile; \Open file for input; return 'true' if success
int FD; \file descriptor
[FD:= FOpen(FileName, 0);
FSet(FD, ^i); \associate device 3 with file; use small buffer (i)
OpenI(3);
return GetErr = 0;
];
 
func GetFileName; \Get FileName from command line, and set any switches
int C, I; \returns 'true' if no errors
[FileName(0):= 0;
C:= ChIn(8);
loop [while C = $20 do C:= ChIn(8); \skip leading spaces (for MS-DOS)
if C = ^- then
[case ChIn(8) of
^s: Offset:= IntIn(8);
^n: Length:= IntIn(8)
other return false; \Error
Backup; \in case number was terminated by CR or EOF
C:= ChIn(8);
]
else if C = $0D\CR\ or C = $1A\EOF\ then quit
else [I:= 0;
repeat FileName(I):= C;
I:= I+1;
C:= ChIn(8);
until C <= $20;
FileName(I):= 0; \terminate string
];
];
return true;
];
 
int I;
[Trap(false); \don't abort program on errors
Offset:= 0; Length:= -1>>1;
if not GetFileName then
[Text(0, "Unrecognized switch. Use -s offset, -n length"); exit];
if not OpenInFile(FileName) then
[Text(0, "File not found"); exit];
Byte:= ChIn(3); \look ahead
Limit:= Offset + Length;
if Limit < 0 then Limit:= -1>>1;
for I:= 0 to Offset-1 do \skip any offset bytes
[GetByte; if GetErr then exit];
repeat DumpHex(I);
I:= I + 16;
until I >= Limit;
HexOut(0, Limit); CrLf(0);
]</syntaxhighlight>
{{out}}
<pre>
hexdump examp16.txt
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
 
hexdump -s 20 examp16.txt -n 20
00000014 6F 00 64 00 65 00 20 00 69 00 73 00 20 00 61 00 |o.d.e. .i.s. .a.|
00000024 20 00 70 00 | .p.|
00000028
</pre>
140

edits