How would I go about creating a hexdump type of view of an imported binary file? for example, (format is

<file offset> | <hex contents> | <ascii representation>)

enter image description here

Which I could then perform an analysis on top of.


I have gotten pretty close with the following

hexdata = ReadByteArray["..file"];

hexshowAmount = 80;
hex = (hexdata[[1 ;; hexshowAmount]] // Normal);

tbl = hex // Partition[#, UpTo@16] &;
nrows = Length@tbl;
ncols = Length@tbl[[1]];

myFunc[val_] := PrimeQ[val]; (* Must Return True or False *)

labels = Flatten@
  Table[If[myFunc[tbl[[i]][[j]]], {i, j} -> Hue[RandomReal[], .2, .9],
     Nothing], {i, nrows}, {j, ncols}];

tbl // Grid[Map[BaseForm[#, 16] &, #, {2}], Frame -> All, 
   Alignment -> Center, Spacings -> {1, 1}, 
   Background -> {None, None, labels}, ItemSize -> {2, 2}] &

Open for improvements though.


1 Answer 1


How about

ba = BinaryReadList["file", "UnsignedInteger16"];
baHex = IntegerString[#, 16] & /@ ba

Multicolumn[baHex, Sequence[16, Appearance -> "Horizontal"]]
  • $\begingroup$ Ah well for my computer I need to use "UnsignedInteger8" (unless you intended for the 2 byte groupings--however this returns the hexdump 2 bytes at a time in a little endian interpretation and not as a raw byte). In any case ReadByteArray works for this portion of the question. $\endgroup$
    – skyfire
    Commented Dec 9, 2020 at 19:45
  • 1
    $\begingroup$ @skyfire The hexdump image in the question is for 2 bytes, so I assumed that is what was wanted. $\endgroup$ Commented Dec 9, 2020 at 20:13
  • $\begingroup$ I know, I just wanted to comment on how doing so in this way interprets reinterprets the bytes as in little endian. i.e. each of the 2 bytes read in is being read as a "short" in C language lingo. This is not the same as the ordering of bytes as they occur within the file. Every 2 bytes will be swapped with how they occur within the file. $\endgroup$
    – skyfire
    Commented Dec 10, 2020 at 5:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.