Challenge
Create a console program to display each byte of a file.
Winning
Since this is code-golf, 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);
- No loopholes please;
Example
test.txt (ends with LF)
Hello World!
$ ./hexdump.exe test.txt
48 65 6C 6C 6F 20 57 6F 72 6C 64 21 0A
29 Answers 29
C (gcc) on *nix, (削除) 73 (削除ここまで) 71 bytes
i;main(c,v)int**v;{for(c=open(v[1],0);read(c,&i,1);printf("%02X ",i));}
-2 bytes thanks to Johan du Toit
This relies on O_RDONLY == 0 and on int_one == 1 where int int_one; *(char*)&int_one = 1;.
PowerShell, (削除) 45 (削除ここまで) 40 bytes
"$(gc $args -ra|% *ay|%{'{0:X2}'-f+$_})"
-5 bytes thanks to mazzy
-
-
1\$\begingroup\$ I learned this from the Kevin Cruijssen solution. \$\endgroup\$Andrei Odegov– Andrei Odegov2019年07月10日 08:41:31 +00:00Commented Jul 10, 2019 at 8:41
PHP, (削除) 60 (削除ここまで) (削除) 59 (削除ここまで) 54 bytes
<?=wordwrap(bin2hex(implode(file($argv[1]))),2,' ',1);
- -1 byte thanks to manassehkatz
- -5 bytes thanks to Blackhole
-
1\$\begingroup\$ 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. \$\endgroup\$manassehkatz-Moving 2 Codidact– manassehkatz-Moving 2 Codidact2019年07月10日 04:34:03 +00:00Commented Jul 10, 2019 at 4:34 -
3\$\begingroup\$ Use
implode(file($x))instead offile_get_contents($x)(-4 bytes). \$\endgroup\$Blackhole– Blackhole2019年07月10日 13:45:10 +00:00Commented Jul 10, 2019 at 13:45 -
3\$\begingroup\$ And
wordwrap(), with1as the last parameter, is one byte shorter thanchunk_split(). \$\endgroup\$Blackhole– Blackhole2019年07月10日 13:54:24 +00:00Commented Jul 10, 2019 at 13:54
-
\$\begingroup\$ Does this read the contents of a file given a file-path as program argument? Based on the TIO it seems to just read from STDIN, but I don't know Ruby well-enough to say it's incorrect. \$\endgroup\$Kevin Cruijssen– Kevin Cruijssen2019年07月10日 18:33:56 +00:00Commented Jul 10, 2019 at 18:33
-
1\$\begingroup\$ @KevinCruijssen Yes, it takes the file path(s) as program argument. If there are no arguments,
$<switches to reading from STDIN instead. \$\endgroup\$Value Ink– Value Ink2019年07月13日 01:14:34 +00:00Commented Jul 13, 2019 at 1:14
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
-
\$\begingroup\$ What's the rationale behind using
interfaceinstead ofclass? \$\endgroup\$JakeDot– JakeDot2019年07月10日 10:06:48 +00:00Commented Jul 10, 2019 at 10:06 -
4\$\begingroup\$ @JakeDot main is required to be public, interface methods are always public,
interfaceis shorter thanclass+public. \$\endgroup\$Grimmy– Grimmy2019年07月10日 11:00:56 +00:00Commented Jul 10, 2019 at 11:00 -
3\$\begingroup\$ With Java 11, you can use
Path.ofinstead ofPaths.get\$\endgroup\$Holger– Holger2019年07月10日 12:02:24 +00:00Commented Jul 10, 2019 at 12:02 -
1\$\begingroup\$ @Holger Thanks! :) \$\endgroup\$Kevin Cruijssen– Kevin Cruijssen2019年07月10日 12:27:14 +00:00Commented Jul 10, 2019 at 12:27
-
2\$\begingroup\$ @Grimy since Java 9, interface methods are not always
public, but they arepublicunless explicitly declaredprivate. \$\endgroup\$Holger– Holger2019年07月10日 12:30:43 +00:00Commented Jul 10, 2019 at 12:30
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()))
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.
-
\$\begingroup\$ Can save a few bytes with
'%02X'%ord(i)instead of slicing the output of hex \$\endgroup\$Mostly Harmless– Mostly Harmless2019年07月09日 23:22:26 +00:00Commented Jul 9, 2019 at 23:22 -
\$\begingroup\$ @MostlyHarmless Done! -11 bytes. Thanks! \$\endgroup\$mprogrammer– mprogrammer2019年07月10日 00:37:07 +00:00Commented Jul 10, 2019 at 0:37
-
\$\begingroup\$ how about '%02X' instead of '%02x', and get rid of
.upper()\$\endgroup\$President James K. Polk– President James K. Polk2019年07月10日 00:50:30 +00:00Commented Jul 10, 2019 at 0:50 -
\$\begingroup\$ You can save the bytes from the
import sysby usingraw_input()as the filename instead; rules allow user prompting. \$\endgroup\$Blue– Blue2019年07月10日 12:11:44 +00:00Commented Jul 10, 2019 at 12:11 -
\$\begingroup\$ @Blue Thanks! And it's even shorter in Python 3, where you can just do
input()\$\endgroup\$mprogrammer– mprogrammer2019年07月10日 15:32:07 +00:00Commented 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
⎕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
-
1\$\begingroup\$ @facepalm42 It very much is in hexadecimal. E.g.
His 72, which is 4×16¹+8×160 or [4,8]16. Hence the first column in the example reads[4,8]. \$\endgroup\$Adám– Adám2019年07月09日 10:28:29 +00:00Commented Jul 9, 2019 at 10:28 -
\$\begingroup\$ Oh, I completely forgot! Sorry. \$\endgroup\$mekb– mekb2019年07月09日 10:29:34 +00:00Commented 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ドル`
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.
-
\$\begingroup\$ Minor reductions:
xxd -u -p 1ドル|fold -2|tr \\n \. \$\endgroup\$manatwork– manatwork2019年07月09日 13:07:49 +00:00Commented Jul 9, 2019 at 13:07 -
\$\begingroup\$ Thanks, any idea how to get the
\nand\to work in the 'this' link version? EDIT: I added another escape character. \$\endgroup\$Jonathan Allan– Jonathan Allan2019年07月09日 13:25:49 +00:00Commented Jul 9, 2019 at 13:25 -
\$\begingroup\$ If I understand you correctly, you just want to change from double quotes to single quotes: Try it online! \$\endgroup\$manatwork– manatwork2019年07月09日 13:41:30 +00:00Commented Jul 9, 2019 at 13:41
-
\$\begingroup\$ Awesome thank you! \$\endgroup\$Jonathan Allan– Jonathan Allan2019年07月09日 13:51:22 +00:00Commented Jul 9, 2019 at 13:51
-
\$\begingroup\$
xxd -c1 -p -u 1ドル|tr \\n \\$\endgroup\$spuck– spuck2019年07月10日 04:35:24 +00:00Commented Jul 10, 2019 at 4:35
Kotlin, (削除) 130 (削除ここまで) (削除) 127 (削除ここまで) (削除) 104 (削除ここまで) (削除) 93 (削除ここまで) 92 bytes
fun main(a:Array<String>){java.io.File(a[0]).readBytes().forEach{print("%02X ".format(it))}}
Edit: -11 bytes thanks to @ChrisParton
Edit: Working TIO
Edit: -1 byte thanks to @KevinCruijssen
-
1\$\begingroup\$ Could you ditch the import and reference
Fileasjava.io.Fileinstead? \$\endgroup\$Chris Parton– Chris Parton2019年07月10日 05:43:46 +00:00Commented Jul 10, 2019 at 5:43 -
\$\begingroup\$ @ChrisParton right you are, thanks! \$\endgroup\$Quinn– Quinn2019年07月10日 12:59:52 +00:00Commented Jul 10, 2019 at 12:59
-
\$\begingroup\$ Here a working TIO. You can use
./.input.tioas file-path argument, and it will use the STDIN as file-content. :) \$\endgroup\$Kevin Cruijssen– Kevin Cruijssen2019年07月10日 18:36:34 +00:00Commented Jul 10, 2019 at 18:36 -
\$\begingroup\$ @KevinCruijssen thanks! just updated answer \$\endgroup\$Quinn– Quinn2019年07月10日 18:40:11 +00:00Commented Jul 10, 2019 at 18:40
-
1\$\begingroup\$ 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. \$\endgroup\$Kevin Cruijssen– Kevin Cruijssen2019年07月10日 19:51:58 +00:00Commented 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(' '));}
-6 bytes because I forgot to reduce variable names
-
\$\begingroup\$ +1 for dart. Such an underrated language. \$\endgroup\$vasilescur– vasilescur2019年07月09日 14:26:26 +00:00Commented Jul 9, 2019 at 14:26
-
\$\begingroup\$ Hard to golf with, since it's basically JS without the very lax type system \$\endgroup\$Elcan– Elcan2019年07月09日 14:27:51 +00:00Commented 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
-
1\$\begingroup\$ A little bit shorter:
import Data.ByteStringplusmain=getArgs>>=Data.ByteString.readFile.(!!0)>>=mapM_(printf"%02X ").unpack. \$\endgroup\$nimi– nimi2019年07月09日 18:07:52 +00:00Commented 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!()}
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
@*ARGS[0]is the first command-line argument..IOturns that (presumed) filename into anIO::Pathobject..slurp(:bin)reads the entire file into aBufbuffer of bytes. (Without the:binthe file contents would be returned as a Unicode string.).listreturns a list of the byte values from the buffer..fmt('%02X')is aListmethod that formats the elements of the list using the given format string, then joins them with spaces. (Convenient!).sayprints that string.
-
\$\begingroup\$ Based on the Python answer, a TIO Link is in fact quite possible. \$\endgroup\$Draco18s no longer trusts SE– Draco18s no longer trusts SE2019年07月09日 21:35:34 +00:00Commented Jul 9, 2019 at 21:35
-
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: enter image description here
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 withtoUpperCase(), and that cost 13 bytes.)
-
\$\begingroup\$ using
...for an array is actually pretty cool \$\endgroup\$mekb– mekb2021年05月08日 13:47:04 +00:00Commented 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));}
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()))
-
\$\begingroup\$ you mean probably
sys.argv[1]. withsys.argv[0]this script works more like a quine ;-) \$\endgroup\$anion– anion2019年07月10日 13:22:52 +00:00Commented Jul 10, 2019 at 13:22 -
\$\begingroup\$ @anion: oops, haha, fixing ... \$\endgroup\$President James K. Polk– President James K. Polk2019年07月10日 13:32:09 +00:00Commented 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
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
1+ \ add 1 to the current address value
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]
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
BinaryReadList@... reads the named file into a list of bytes
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
Pyth, 12 bytes
jdcr1.Hjb'w2
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 - (削除) 235 (削除ここまで) (削除) 213 (削除ここまで) (削除) 203 (削除ここまで) (削除) 191 (削除ここまで) (削除) 175 (削除ここまで) 140 bytes
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") + " ");
}
}
}
}
-
1\$\begingroup\$ 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")} ");}}}} \$\endgroup\$PmanAce– PmanAce2019年07月17日 15:05:32 +00:00Commented Jul 17, 2019 at 15:05
-
\$\begingroup\$ @PmanAce If you remove some of the whitespace, it gets down to 175. \$\endgroup\$mekb– mekb2019年07月18日 05:58:20 +00:00Commented Jul 18, 2019 at 5:58
05AB1E, 18 bytes
IvyÇh2j' 0.:' Jvy?
Explanation:
IvyÇh2j' 0.:' Jvy?
Iv Loop through each character in input
y Push current character
Ç ASCII value
h Convert to hexadecimal
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.
hello.txttext file as an example as an input and what the expected output should be. For instance, if thehello.txtcontained simply the wordhellowith a line break, how would this be expressed in the output? Are you grouping the bytes in 16-bit,32-bit or 64-bit words? Or is each byte expressed as two-digit hex? Are spaces acceptable after each byte as hex, or after each x-bit word? Do you require an0xpre-fix for each byte? \$\endgroup\$