Hex dump: Difference between revisions
m →{{header|Python}}: Fix offset and final byte count when skipping bytes. Show example of using offset and length. |
m →{{header|Julia}}: add file usage |
||
Line 413: | Line 413: | ||
=={{header|Julia}}== |
=={{header|Julia}}== |
||
<syntaxhighlight lang="julia">function baseddump(data |
<syntaxhighlight lang="julia">function baseddump(data::Vector{UInt8}; base = 16, offset = 0, len = -1, displayadjust = 0) |
||
@assert 2 <= base <= 16 "display base $base not supported" |
|||
len = len < 0 ? length(data) : min(len, length(data)) |
|||
bytes = data[begin+offset:len] |
|||
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 |
|||
⚫ | |||
⚫ | |||
pad = base == 16 ? 2 : base == 10 ? 3 : 8) * " ", chunk) |
|||
⚫ | |||
vstr = join(values[begin:begin+min(halflen, chunklen)-1]) |
|||
⚫ | |||
⚫ | |||
s1 = join(values[begin:begin+min(halflen, chunklen)-1]) |
|||
⚫ | |||
⚫ | |||
s1 *= midpad * join(values[begin+halflen:end]) |
|||
⚫ | |||
end |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
end |
|||
⚫ | |||
⚫ | |||
function baseddump(data; base = 16, offset = 0, len = -1) |
|||
return baseddump(vec(collect(reinterpret(UInt8, data))); base, offset, len) |
|||
⚫ | |||
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 |
end |
||
hexdump(data; offset = 0, len = -1) = baseddump(data; offset, len) |
hexdump(data; offset = 0, len = -1) = baseddump(data; offset, len) |
||
xxd(data; offset = 0, len = -1) = baseddump(data |
xxd(data; offset = 0, len = -1) = baseddump(data; base = 2, offset, len) |
||
decdump(data; offset = 0, len = -1) = baseddump(data |
decdump(data; offset = 0, len = -1) = baseddump(data; base = 10, offset, len) |
||
const tstr = b"Rosetta Code is a programming chrestomathy site 😀." |
const tstr = b"Rosetta Code is a programming chrestomathy site 😀." |
||
Line 445: | Line 459: | ||
print("\ndecdump of utf-16 string "), display(String(tstr)) |
print("\ndecdump of utf-16 string "), display(String(tstr)) |
||
decdump(utf16) |
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}} |
</syntaxhighlight>{{out}} |
||
<pre> |
<pre> |
||
Line 455: | Line 474: | ||
00000050 74 00 68 00 79 00 20 00 73 00 69 00 74 00 65 00 |t.h.y. .s.i.t.e.| |
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 | .=.....| |
00000060 20 00 3d d8 00 de 2e 00 | .=.....| |
||
00000068 |
00000068 |
||
xxd of utf-16 string "Rosetta Code is a programming chrestomathy site 😀." |
xxd of utf-16 string "Rosetta Code is a programming chrestomathy site 😀." |
||
00000000 11111111 11111110 01010010 00000000 01101111 00000000 |
00000000 11111111 11111110 01010010 00000000 01101111 00000000 |..R.o.| |
||
00000006 01110011 00000000 01100101 00000000 01110100 00000000 |
00000006 01110011 00000000 01100101 00000000 01110100 00000000 |s.e.t.| |
||
0000000c 01110100 00000000 01100001 00000000 00100000 00000000 |
0000000c 01110100 00000000 01100001 00000000 00100000 00000000 |t.a. .| |
||
00000012 01000011 00000000 01101111 00000000 01100100 00000000 |
00000012 01000011 00000000 01101111 00000000 01100100 00000000 |C.o.d.| |
||
00000018 01100101 00000000 00100000 00000000 01101001 00000000 |
00000018 01100101 00000000 00100000 00000000 01101001 00000000 |e. .i.| |
||
0000001e 01110011 00000000 00100000 00000000 01100001 00000000 |
0000001e 01110011 00000000 00100000 00000000 01100001 00000000 |s. .a.| |
||
00000024 00100000 00000000 01110000 00000000 01110010 00000000 |
00000024 00100000 00000000 01110000 00000000 01110010 00000000 | .p.r.| |
||
0000002a 01101111 00000000 01100111 00000000 01110010 00000000 |
0000002a 01101111 00000000 01100111 00000000 01110010 00000000 |o.g.r.| |
||
00000030 01100001 00000000 01101101 00000000 01101101 00000000 |
00000030 01100001 00000000 01101101 00000000 01101101 00000000 |a.m.m.| |
||
00000036 01101001 00000000 01101110 00000000 01100111 00000000 |
00000036 01101001 00000000 01101110 00000000 01100111 00000000 |i.n.g.| |
||
0000003c 00100000 00000000 01100011 00000000 01101000 00000000 |
0000003c 00100000 00000000 01100011 00000000 01101000 00000000 | .c.h.| |
||
00000042 01110010 00000000 01100101 00000000 01110011 00000000 |
00000042 01110010 00000000 01100101 00000000 01110011 00000000 |r.e.s.| |
||
00000048 01110100 00000000 01101111 00000000 01101101 00000000 |
00000048 01110100 00000000 01101111 00000000 01101101 00000000 |t.o.m.| |
||
0000004e 01100001 00000000 01110100 00000000 01101000 00000000 |
0000004e 01100001 00000000 01110100 00000000 01101000 00000000 |a.t.h.| |
||
00000054 01111001 00000000 00100000 00000000 01110011 00000000 |
00000054 01111001 00000000 00100000 00000000 01110011 00000000 |y. .s.| |
||
0000005a 01101001 00000000 01110100 00000000 01100101 00000000 |
0000005a 01101001 00000000 01110100 00000000 01100101 00000000 |i.t.e.| |
||
00000060 00100000 00000000 00111101 11011000 00000000 11011110 |
00000060 00100000 00000000 00111101 11011000 00000000 11011110 | .=...| |
||
00000066 00101110 00000000 |
00000066 00101110 00000000 |..| |
||
00000068 |
00000068 |
||
Line 491: | Line 510: | ||
00000064 000 222 046 000 |....| |
00000064 000 222 046 000 |....| |
||
00000068 |
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> |
</pre> |
||
Revision as of 18:11, 2 November 2023
A hex dump is a textual representation of bytes in a file, and hexdump is a command-line tool that can dump bytes from a file in a variety of formats, including hexadecimal, octal and ASCII.
hexdump's canonical format displays, on each line:
- a byte offset in hexadecimal,
- up to 16 bytes in hexadecimal, separated by spaces, with an extra space between the 8th and 9th byte,
- the same 16 bytes interpreted as ASCII characters, with non-printing and non-ascii characters replaced with a dot (
.
), surrounded by pipes (|
).
The last line of output shows a final byte count, also in hexadecimal.
For example, the string "Rosetta Code is a programming chrestomathy site 😀." encoded in UTF-16 (little-endian - the first two bytes are the byte order mark), displayed in the canonical format is:
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
- Task
Implement a hexdump-like program that:
- outputs in the canonical format,
- takes an optional offset in bytes from which to start,
- takes an optional length in bytes after which it will stop.
Demonstrate your implementation by showing the canonical hex dump of the example above, plus any other examples you find useful.
- Stretch
xxd is another command-line tool similar to hexdump. It offers a binary mode where bytes are displayed in bits instead of hexadecimal.
Implement a binary mode. For this task, in binary mode, the example above should be displayed like this:
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
Other hexdump/xxd features and a command line interface to your program are optional.
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;
}
- Output:
Hexadecimal output:
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 output:
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
FreeBASIC
Code adapted from https://github.com/jlaasonen/hexfile
The code is from Jussi Laasonen (https://jlaasonen.me)
'Usage: hexfile [-i <first index>] <file>
Const bytesPerLine = 16
Const tabWidth = 3
Const fileIndexWidth = 8
Const firstIndex = 1
Const asciiLowerBound = 31
Const asciiUpperBound = 127
Const emptyByte = " "
Const emptyAsciiByte = " "
Const nonAsciiByte = "."
Enum Keys Explicit
Up = &H48FF
PageUp = &H49FF
Down = &H50FF
PageDown = &H51FF
Home = &H47FF
End_ = &H4FFF
Esc = 27
End Enum
Sub GetLine(Byval fileIndex As Longint, Byval fileNumber As Integer, bytes() As Ubyte)
Dim bytesread As Uinteger
If Get(#fileNumber,fileIndex,bytes(), ,bytesread) = 0 And bytesread > 0 Then
Redim Preserve bytes(bytesread)
Else
Erase bytes
End If
End Sub
Function ByteToAscii(Byval byte_ As Ubyte) As String
Return Iif(byte_ > asciiLowerBound And byte_ < asciiUpperBound, Chr(byte_), nonAsciiByte)
End Function
Function MakeLine(Byval firstIndexDisplay As Longint, Byval fileIndex As Longint, bytes() As Ubyte) As String
If Ubound(bytes) < Lbound(bytes) Then Return "" End If
Dim hexBytes As String = ""
Dim asciiBytes As String = ""
For byteIndex As Integer = Lbound(bytes) To Lbound(bytes) + bytesPerLine - 1
If byteIndex <= Ubound(bytes) Then
Dim byte_ As Ubyte = bytes(byteIndex)
asciiBytes += ByteToAscii(byte_)
hexBytes += Hex(byte_, 2)
Else
asciiBytes += emptyAsciiByte
hexBytes += emptyByte
End If
hexBytes += Iif(byteIndex = (bytesPerLine\2)-1, " ", " ")
Next
Dim displayIndex As Const Longint = fileIndex - firstIndex + firstIndexDisplay
Return Space(1) + Hex(displayIndex,fileIndexWidth) + Space(tabWidth) +_
hexBytes + Space(tabWidth-1) + asciiBytes
End Function
Sub DumpInteractive(Byval fileNumber As Integer, Byval firstIndexDisplay As Longint)
Dim consoleDimensions As Integer = Width()
Dim linesPerPage As Integer = Hiword(consoleDimensions)
Dim bytesPerPage As Integer = bytesPerline * linesPerPage
Dim numberOfFullLines As Longint = (Lof(fileNumber)-1) \ bytesPerLine
Dim lastLineIndex As Longint = numberOfFullLines * bytesPerLine + firstIndex
Dim lastPageIndex As Longint = lastLineIndex - bytesPerPage + bytesPerLine
If lastPageIndex < firstIndex Then lastPageIndex = firstIndex End If
Cls
Dim input_ As Long
Dim fileIndex As Longint = firstIndex
Do
Locate 1,1,0
For lineNumber As Integer = 1 To linesPerPage
Dim lineIndex As Longint = fileIndex + (lineNumber-1)*bytesPerLine
Redim bytes(bytesPerLine) As Ubyte
GetLine(lineIndex, fileNumber, bytes())
Dim lineText As String = MakeLine(firstIndexDisplay, lineIndex, bytes())
If lineNumber = linesPerPage Then
Print lineText;
Else
Print lineText
End If
Next
input_ = Getkey
Select Case As Const input_
Case Keys.PageUp
fileIndex -= bytesPerPage
Case Keys.Up
fileIndex -= bytesPerLine
Case Keys.PageDown
fileIndex += bytesPerPage
Case Keys.Down
fileIndex += bytesPerLine
Case Keys.Home
fileIndex = firstIndex
Case Keys.End_
fileIndex = lastPageIndex
End Select
If fileIndex < firstIndex Then fileIndex = firstIndex
If fileIndex > lastPageIndex Then fileIndex = lastPageIndex
Loop Until input_ = Keys.Esc
End Sub
Sub PrintUsage()
Const firstColumn = 4
Const secondColumn = 12
Dim controls(0 To ..., 2) As String = {_
{"Down", "Scroll down one line."},_
{"Up", "Scroll up one line."},_
{"PgDown", "Show next page."},_
{"PgUp", "Show previos page."},_
{"End", "Jump to the end of the file."},_
{"Home", "Jump to the beginning of the file."},_
{"Esc", "Quit."}_
}
Print "hexfile - a simple commandline hexdumper"
Print " (c) 2021-2023 Jussi Laasonen. Licensed under the MIT license."
Print " See https://github.com/jlaasonen/hexfile for full license."
Print !"\nUsage: hexfile [-i <first index>] <file>"
Print !"\nDisplays an interactive hex dump the file."
Print !"\nControls:"
For row As Integer = Lbound(controls) To Ubound(controls)
Print Tab(firstColumn);controls(row,0);Tab(secondColumn);controls(row,1)
Next
Print !"\nOptions:"
Print " -i <first index>"
Print " Starts the displayed file index from the given index. Defaults to 0."
Print " The value is expected in decimal format. For binary, octal or hexadecimal"
Print " Use prefix &B, &O or &H respectively."
End Sub
Const fileIndexArgument = "-i"
Dim fileName As String = ""
Dim firstIndexDisplay As Longint = 0
If __fb_argc__ = 2 Then
fileName = Command(1)
Elseif __fb_argc__ = 4 And Command(1) = fileIndexArgument Then
firstIndexDisplay = Vallng(Command(2))
fileName = Command(3)
End If
Dim fileNumber As Long = Freefile
If fileName = "" Then
PrintUsage()
Elseif Open(fileName For Binary Access Read As #fileNumber) = 0 Then
DumpInteractive(fileNumber, firstIndexDisplay)
Close(fileNumber)
Else
Print "Failed to open file '" + fileName + "'."
End If
Julia
function baseddump(data::Vector{UInt8}; base = 16, offset = 0, len = -1, displayadjust = 0)
@assert 2 <= base <= 16 "display base $base not supported"
len = len < 0 ? length(data) : min(len, length(data))
bytes = data[begin+offset:len]
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(string(pos, base = 16, pad = 8) * " " * rpad(s1, vl) * "|" * s2 * "|")
pos += chunklen
end
println(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)
- Output:
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
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)
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.
- Output:
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
Python
"""Display bytes in a file like hexdump or xxd."""
import abc
import math
from io import BufferedIOBase
from io import SEEK_END
from itertools import islice
from typing import Iterable
from typing import Iterator
from typing import Sequence
from typing import Tuple
from typing import TypeVar
READ_SIZE = 2048
class Formatter(abc.ABC):
"""Base class for hex dump formatters."""
@abc.abstractmethod
def 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."""
class CanonicalFormatter(Formatter):
bytes_per_line = 16
def format(self, data: Sequence[int]) -> str:
assert len(data) <= 16
hex = f"{bytes(data[:8]).hex(' ')} {bytes(data[8:]).hex(' ')}".ljust(48)
ascii_ = "".join(chr(b) if b > 31 and b < 127 else "." for b in data)
return f"{hex} |{ascii_}|"
class BinaryFormatter(Formatter):
bytes_per_line = 6
def format(self, data: Sequence[int]) -> str:
assert len(data) <= 6
bits = " ".join(bin(b)[2:].rjust(8, "0") for b in data).ljust(53)
ascii_ = "".join(chr(b) if b > 31 and b < 127 else "." for b in data)
return f"{bits} |{ascii_}|"
canonicalFormatter = CanonicalFormatter()
binaryFormatter = BinaryFormatter()
T = TypeVar("T")
def group(it: Iterable[T], n: int) -> Iterator[Tuple[T, ...]]:
"""Split iterable _it_ in to groups of size _n_.
The last group might contain less than _n_ items.
"""
_it = iter(it)
while True:
g = tuple(islice(_it, n))
if not g:
break
yield g
def hex_dump(
f: BufferedIOBase,
*,
skip: int = 0,
length: int = math.inf, # type: ignore
formatter: Formatter = canonicalFormatter,
) -> Iterator[str]:
"""Generate a textual representation of bytes in _f_, one line at a time."""
f.seek(skip)
offset = skip
byte_count = 0
previous_line = ""
squeezing = False # True if the last output line was squeezed
assert length > 0
while byte_count < length:
# Read at most READ_SIZE bytes at a time.
data = f.read(READ_SIZE)
# Stop if we've run out of data.
if not data:
break
# Discard excess bytes if we've overshot length.
if byte_count + len(data) > length:
data = data[: length - byte_count]
# One line per chunk
for chunk in group(data, formatter.bytes_per_line):
line = formatter.format(chunk)
if previous_line == line:
if squeezing is False:
squeezing = True
yield "*"
else:
previous_line = line
squeezing = False
yield f"{offset:0>8x} {line}"
offset += formatter.bytes_per_line
byte_count += len(chunk)
# Final byte count
bytes_in_f = f.seek(0, SEEK_END)
yield f"{min(byte_count + skip, bytes_in_f):0>8x}"
if __name__ == "__main__":
import argparse
parser = argparse.ArgumentParser(
prog="hex_dump.py",
description="Display bytes in a file.",
)
parser.add_argument(
"file",
type=argparse.FileType(mode="rb"),
metavar="FILE",
help="target file to dump",
)
parser.add_argument(
"-b",
"--binary",
action="store_true",
help="display bytes in binary instead of hex",
)
parser.add_argument(
"-s",
"--skip",
type=int,
default=0,
help="skip SKIP bytes from the beginning",
)
parser.add_argument(
"-n",
"--length",
type=int,
default=math.inf,
help="read up to LENGTH bytes",
)
args = parser.parse_args()
formatter = binaryFormatter if args.binary else canonicalFormatter
for line in hex_dump(
args.file,
formatter=formatter,
skip=args.skip,
length=args.length,
):
print(line)
- Output:
$ python hex_dump.py example_utf16.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
$ python hex_dump.py example_utf16.txt -s 20 -n 25
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
$ python hex_dump.py example_utf16.txt -b
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
Wren
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.
import "io" for File
import "./seq" for Lst
import "./fmt" for Fmt
class Dumper {
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
var bc = bytes.count
// remove final \n if present
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
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
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]
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() + (" " * (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)
} else {
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)
} else {
var fmt = "$08x %(bf)$s%(bf)$s $s"
Fmt.print(fmt, offset, line[0...hrl], sp, line[hrl..-1], extra, text)
}
}
}
Fmt.print("$08x", start + bc)
}
static dump(fileName, base, start) { dump(fileName, base, start, -1) }
static dump(fileName, base) { dump(fileName, base, 0, -1) }
}
var fileName = "example_utf16.txt"
for (base in [16, 10, 8, 3, 2]) {
Dumper.dump(fileName, base)
System.print()
}
Dumper.dump(fileName, 16, 2, 24)
- Output:
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.| 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 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.| 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 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