# Shortest hex dumping program

### Challenge

Create a console program to display each byte of a file.

### Winning

Since this is , fewest bytes wins.

### Rules

• Program must be a console application, meaning that it will be ran from some sort of command-line interpreter;
• Every byte must be uppercase hexadecimal, separated by a space, and it must be 2 digits; (put number 0 before it if it has 1 digit)
• File must be read using IO or alternative, and not hard-coded;
• File path must be specified as a command-line argument or a user prompt (like STDIN);

### Example

test.txt (ends with LF)

Hello World!



"$(gc$args -ra|% *ay|%{'{0:X2}'-f+$_})"  Try it online! -5 bytes thanks to mazzy • thanks for ./.input.tio. 40 bytes with CRLF preserved. Jul 10, 2019 at 5:29 • I learned this from the Kevin Cruijssen solution. Jul 10, 2019 at 8:41 # Java 11, 156 154 bytes import java.nio.file.*;interface M{static void main(String[]a)throws Exception{for(int b:Files.readAllBytes(Path.of(a[0])))System.out.printf("%02X ",b);}}  -2 bytes thanks to @Holger. Try it online by using ./.input.tio as argument file-path, which will have a given input as file-content. Explanation: import java.nio.file.*; // Required import for Files and Paths interface M{ // Class static void main(String[]a) // Mandatory main method throws Exception{ // With mandatory thrown clause for the readAllBytes builtin a[0] // Get the first argument Path.of( ) // Get the file using that argument as path Files.readAllBytes( ) // Get all bytes from this file for(int b: ) // Loop over each of them: System.out.printf( // And print the current byte "%02X ",b);}} // As uppercase hexadecimal with leading 0 // and trailing space as delimiter  • What's the rationale behind using interface instead of class? Jul 10, 2019 at 10:06 • @JakeDot main is required to be public, interface methods are always public, interface is shorter than class + public. Jul 10, 2019 at 11:00 • With Java 11, you can use Path.of instead of Paths.get Jul 10, 2019 at 12:02 • @Holger Thanks! :) Jul 10, 2019 at 12:27 • @Grimy since Java 9, interface methods are not always public, but they are public unless explicitly declared private. Jul 10, 2019 at 12:30 # PHP, 6059 54 bytes <?=wordwrap(bin2hex(implode(file($argv[1]))),2,' ',1);

• -1 byte thanks to manassehkatz
• -5 bytes thanks to Blackhole

Try it online!

• Should be able to drop the trailing ?> and save 2 bytes, or if that doesn't work then replace ?> with a semicolon and save 1 byte. Jul 10, 2019 at 4:34
• Use implode(file($x)) instead of file_get_contents($x) (-4 bytes). Jul 10, 2019 at 13:45
• And wordwrap(), with 1 as the last parameter, is one byte shorter than chunk_split(). Jul 10, 2019 at 13:54

# Perl 5 (-aF//), 23 bytes

printf"%02X ",ord for@F


TIO

## Python 3, 59 bytes

-11 bytes thanks to Mostly Harmless!

-8 bytes thanks to James K Polk!

-24 bytes thanks to Blue!

print(' '.join('%02X'%ord(i)for i in open(input()).read()))


Try it online!

This is pretty straightforward; it opens a filename given as input on STDIN, reads it, converts each character to its ASCII value, converts each number to hex, strips off the "0x" that precedes hexademical values in Python, pads the value with a zero if necessary, then joins the values together with spaces.

• Can save a few bytes with '%02X'%ord(i) instead of slicing the output of hex Jul 9, 2019 at 23:22
• @MostlyHarmless Done! -11 bytes. Thanks! Jul 10, 2019 at 0:37
• how about '%02X' instead of '%02x', and get rid of .upper() Jul 10, 2019 at 0:50
• You can save the bytes from the import sys by using raw_input() as the filename instead; rules allow user prompting.
– Blue
Jul 10, 2019 at 12:11
• @Blue Thanks! And it's even shorter in Python 3, where you can just do input() Jul 10, 2019 at 15:32

# APL (Dyalog Unicode), 16 bytes

Anonymous tacit prefix function. Returns (and implicitly prints, if the value isn't otherwise consumed) a two-row matrix with the top 4 bits represented as a decimal number 0–15 in the top row and the bottom 4 bits similarly represented in the bottom row. That is, the matrix has as many columns as the file has bytes.

16 16⊤83 ¯1∘⎕MAP


Try it online!

⎕MAP map the argument filename to an array
∘ with parameters:
¯1 the entire length of the file
83 read as 8-bit integers

16 16⊤ convert (anti-base) to 2-position hexadecimal

• @facepalm42 It very much is in hexadecimal. E.g. H is 72, which is 4×16¹+8×16⁰ or [4,8]₁₆. Hence the first column in the example reads [4,8].
Jul 9, 2019 at 10:28
• Oh, I completely forgot! Sorry.
– mekb
Jul 9, 2019 at 10:29

# Bash,  33  23 bytes

...with a lot of help:
-3 thanks to manatwork
-4 thanks to spuck
-3 thanks to Nahuel Fouilleul

echo xxd -c1 -p -u $1  Try it online! Note that the TIO link above uses input - we can write files locally, so this shows it working as a program taking a file path. • Minor reductions: xxd -u -p$1|fold -2|tr \\n \ . Jul 9, 2019 at 13:07
• Thanks, any idea how to get the \n and \  to work in the 'this' link version? EDIT: I added another escape character. Jul 9, 2019 at 13:25
• If I understand you correctly, you just want to change from double quotes to single quotes: Try it online! Jul 9, 2019 at 13:41
• Awesome thank you! Jul 9, 2019 at 13:51
• xxd -c1 -p -u $1|tr \\n \  Jul 10, 2019 at 4:35 # Kotlin, 13012710493 92 bytes fun main(a:Array<String>){java.io.File(a[0]).readBytes().forEach{print("%02X ".format(it))}}  Try it online! Edit: -11 bytes thanks to @ChrisParton Edit: Working TIO Edit: -1 byte thanks to @KevinCruijssen • Could you ditch the import and reference File as java.io.File instead? Jul 10, 2019 at 5:43 • @ChrisParton right you are, thanks! Jul 10, 2019 at 12:59 • Here a working TIO. You can use ./.input.tio as file-path argument, and it will use the STDIN as file-content. :) Jul 10, 2019 at 18:36 • @KevinCruijssen thanks! just updated answer Jul 10, 2019 at 18:40 • I don't know Kotlin, but the TIO still works if I remove the space at a:Array, so I think you can save a byte. Jul 10, 2019 at 19:51 # Dart, 140 134 bytes import'dart:io';main(a){print(new File(a[0]).readAsBytesSync().map((n)=>n.toRadixString(16).toUpperCase().padLeft(2,'0')).join(' '));}  Try it online! -6 bytes because I forgot to reduce variable names • +1 for dart. Such an underrated language. Jul 9, 2019 at 14:26 • Hard to golf with, since it's basically JS without the very lax type system Jul 9, 2019 at 14:27 # Haskell, 145 143 bytes import System.Environment import Text.Printf import Data.ByteString main=getArgs>>=Data.ByteString.readFile.(!!0)>>=mapM_(printf"%02X ").unpack  • A little bit shorter: import Data.ByteString plus main=getArgs>>=Data.ByteString.readFile.(!!0)>>=mapM_(printf"%02X ").unpack. – nimi Jul 9, 2019 at 18:07 # Rust, 141 bytes (contributed version) use std::{io::*,fs::*,env::*};fn main(){for x in File::open(args().nth(1).unwrap()).unwrap().bytes(){print!("{:02X} ",x.unwrap())}println!()}  # Rust, 151 bytes (original version) fn main(){std::io::Read::bytes(std::fs::File::open(std::env::args().nth(1).unwrap()).unwrap()).map(|x|print!("{:02X} ",x.unwrap())).count();println!()}  • -10 bytes: TIO Jul 10, 2019 at 16:04 ## bash+Stax, 6+4+1=11 bytes This is complete theory craft at this point. You can't actually run this. If everything works according to its spec this would work, but not everything does yet. The bash script is ]<$1


and the stax program must be compiled and saved to ] is

╛↕ßú┼_


Set your character set to ISO 8859-1 (Windows-1252 won't work here) and go

Unpacked and explained

_          push all input as a single array
F          run the rest of the program for each element of the array
|H        write the hex of the byte to standard output
|         write a space to standard output


# Emojicode, 186 162 bytes

📦files🏠🏁🍇🔂b🍺📇🐇📄🆕🔡👂🏼❗️❗️🍇👄📫🍪🔪🔡🔢b❗️➕256 16❗️1 2❗️🔤 🔤🍪❗️❗️🍉🍉


Try it online here.

Ungolfed:

📦 files 🏠  💭 Import the files package into the default namespace
🏁 🍇  💭 Main code block
🔂 b  💭 For each b in ...
🍺  💭 (ignoring IO errors)
📇 🐇 📄  💭 ... the byte representation of the file ...
🆕 🔡 👂🏼  💭 ... read from user input:
❗️ ❗️ 🍇
👄  💭 Print ...
📫  💭 ... in upper case (numbers in bases > 10 are in lower case) ...
🍪  💭 ... the concatenation of:
🔪 🔡 🔢 b ❗️ ➕ 256  💭 b + 256 (this gives the leading zero in case the hex representation of b is a single digit) ...
16  💭 ... represented in hexadecimal ...
❗️
1 2  💭 ... without the leading one,
❗️
🔤 🔤  💭 ... and a space
🍪
❗️❗️
🍉
🍉


# Perl 6, 45 bytes

@*ARGS[0].IO.slurp(:bin).list.fmt('%02X').say


Try it online!

• @*ARGS[0] is the first command-line argument.
• .IO turns that (presumed) filename into an IO::Path object.
• .slurp(:bin) reads the entire file into a Buf buffer of bytes. (Without the :bin the file contents would be returned as a Unicode string.)
• .list returns a list of the byte values from the buffer.
• .fmt('%02X') is a List method that formats the elements of the list using the given format string, then joins them with spaces. (Convenient!)
• .say prints that string.
• Based on the Python answer, a TIO Link is in fact quite possible. Jul 9, 2019 at 21:35
• Some rearranging can remove the .list for 41 bytes
– Jo King
Jul 13, 2019 at 4:01

# Node.js, 118 bytes

console.log([...require("fs").readFileSync(process.argv[2])].map(y=>(y<16?0:"")+y.toString(16).toUpperCase()).join )


What the result looks like:

Btw the content of test.txt in the example is as follows:

做乜嘢要輸出大楷姐，搞到要加番toUpperCase()去轉番，咁就13byte啦。

(Why on earth is upper-case output necessary. I had to add the conversion with toUpperCase(), and that cost 13 bytes.)

• using ... for an array is actually pretty cool
– mekb
May 8, 2021 at 13:47

# D, 98 Bytes

import std;void main(string[]s){File(s[1]).byChunk(9).joiner.each!(a=>writef("%02X ",a.to!byte));}


Try it Online!

# Python 3, 75 bytes

Mostly a copy of Maxwell's python 2 answer.

import sys
print(' '.join('%02X'%b for b in open(sys.argv[1],'rb').read()))

• you mean probably sys.argv[1]. with sys.argv[0] this script works more like a quine ;-) Jul 10, 2019 at 13:22
• @anion: oops, haha, fixing ... Jul 10, 2019 at 13:32

# Racket, 144 bytes

This submission does output a trailing space, and no trailing newline. Let me know if this is considered a loophole :)

(command-line #:args(f)(for([b(call-with-input-file f port->bytes)])(printf"~a "(string-upcase(~r b #:base 16 #:min-width 2 #:pad-string"0")))))


## Cleaned up

(command-line #:args (f)
(for ([b (call-with-input-file f port->bytes)])
(printf "~a "
(string-upcase
(~r b #:base 16 #:min-width 2 #:pad-string "0")))))


# Forth (gforth), 71 bytes

: f slurp-file hex 0 do dup c@ 0 <# # # #> type space 1+ loop ;
1 arg f


Try it online!

TIO has 3 arg in the last line because TIO passes "-e bye" to the command line parser before passing in the code

### Code Explanation

: f             \ start a function definition
slurp-file    \ open the file indicated by the string on top of the stack,
\ then put its contents  in a new string on top of the stack
hex           \ set the interpreter to base 16
0 do          \ loop from 0 to file-length - 1 (inclusive)
dup c@      \ get the character value from the address on top of the stack
0 <# # # #> \ convert to a double-length number then convert to a string of length 2
type        \ output the created string
space       \ output a space
loop          \ end the loop
;               \ end the word definition
1 arg f         \ get the filename from the first command-line argument and call the function


## Javascript, 155 bytes

for(b=WScript,a=new ActiveXObject("Scripting.FileSystemObject").OpenTextFile(b.Arguments(0));;b.echo(('0'+a.read(1).charCodeAt(0).toString(16)).slice(-2)))

## VBScript, 143 bytes

set a=CreateObject("Scripting.FileSystemObject").OpenTextFile(WScript.Arguments(0)):while 1 WScript.echo(right("0"+Hex(Asc(a.read(1))),2)):wend

# Wolfram Language (Mathematica), 94 89 bytes

Print@ToUpperCase@StringRiffle@IntegerString[BinaryReadList@Last@$ScriptCommandLine,16,2]  Try it online! The code is quite self-explanatory because of the long command names. It should be read mostly from right to left: $ScriptCommandLine       is a list of {scriptname, commandlinearg1, commandlinearg2, ...}
Last@...                 extracts the last command-line argument
IntegerString[...,16,2]  converts each byte to a 2-digit hex string (lowercase)
StringRiffle@...         converts this list of strings into a single string with spaces
ToUpperCase@...          converts the string to uppercase
Print@...                prints the result to stdout


# Gema, 45 characters

?=@fill-right{00;@radix{10;16;@char-int{?}}}


Sample run:

bash-5.0$gema '?=@fill-right{00;@radix{10;16;@char-int{?}}} ' <<< 'Hello World!' 48 65 6C 6C 6F 20 57 6F 72 6C 64 21 0A  Try it online! # Pyth, 12 bytes jdcr1.Hjb'w2  Try it online! Takes input as user prompt (no way to access command-line arguments AFAIK). jd # join on spaces c 2 # chop into pieces of length 2 r1 # convert to uppercase .H # convert to hex string, interpreting as base 256 (*) jb # join on newlines ' # read file as list of lines w # input()  (*) I'm not 100% sure if this is intended, but one base 256 digit (as in, one character), will always convert into exactly 2 hex digits, eliminating the need to pad with zeroes. ### C#.NET Framework 4.7.2 - 235213203191175 140 bytes Try it online! using System.IO;class P{static void Main(string[]a){foreach(var b in File.ReadAllBytes(a[0])){System.Console.Write(b.ToString("X2")+" ");}}}  using System; using System.IO; namespace hexdump { class Program { static void Main(string[] args) { // Read the bytes of the file byte[] bytes = File.ReadAllBytes(args[0]); // Loop through all the bytes and show them foreach (byte b in bytes) { // Show the byte converted to hexadecimal Console.Write(b.ToString("X2") + " "); } } } }  • I think the following will save some bytes (now 181 I think): using System.IO;class P{static void Main(string[] a){if(a.Length>0 && File.Exists(a[0])){foreach(var b in File.ReadAllBytes(a[0])){System.Console.Write($"{b.ToString("X2")} ");}}}} Jul 17, 2019 at 15:05
• @PmanAce If you remove some of the whitespace, it gets down to 175.
– mekb
Jul 18, 2019 at 5:58

# 05AB1E, 18 bytes

IvyÇh2j' 0.:' Jvy?


Try it online!

Explanation:

IvyÇh2j' 0.:' Jvy?
Iv                 Loop through each character in input
y                Push current character
Ç               ASCII value
2j            Pad with at least 2 spaces
' 0.:       Replace all spaces with 0s
' J    Add space to end
vy? Convert to string and print
IvyÇh2j' 0.:' Jvy?


# Free Pascal, 81 B

var c:char;begin while not EOF do begin read(c);write(hexStr(ord(c),2):3)end end.


Note, output has a leading blank and no trailing newline:
 48 65 6C 6C 6F 20 57 6F 72 6C 64 21 0A

−3 B: By default the FPC (FreePascal Compiler) has the “FPC compiler compatibility mode” selected. When compiling in FPC’s “ISO standard compiler compatibility mode” the file buffer variables (here specifically input^) and “low-level” I/O routines like get become available. The following code needs to be compiled with fpc ‑MISO sourceCode.pas:

begin while not EOF do begin write(hexStr(ord(input^),2):3);get(input)end end.


−10 B: Furthermore, if input was guaranteed to be non-empty, we can use a repeat … until … loop instead of the bulky while … do begin … end:

begin repeat write(hexStr(ord(input^),2):3);get(input)until EOF end.