Kaleidoscope Blog

Compare binary data using Kaleidoscope

From time to time, we hear that users want to compare files as binary using Kaleidoscope.

Why compare files as binary?

There are a number of valid reasons why you might want to compare files as binary data. The most obvious case is for file types that are binary by nature, such as executables. For an executable, there is no easy way to “show” the contents of the file to a human, as executable files are typically read by computers, not humans. That’s why Kaleidoscope doesn’t open executable files.

As a developer, you might want to check aspects of an executable down to the bit-level detail. So you need a way to look at those bits and even check two sets of bits for differences.

Then there’s another use case, more closely related to the core functionality of Kaleidoscope: files that look identical in Kaleidoscope might not be identical on disk. Consider two very simple text files Mac.txt and Win.txt. When comparing them in Kaleidoscope, their text content is the same, with no change to spot:

Comparing the Bavarian greeting “Griaß di!” stored in two files. As can be seen, the text content is identical.

But when comparing the raw binary data of those files, something curious surfaces:

Turns out the binary data of both files is actually different, despite the text being equal.

One byte is different. Why is that? Of course, this is an example that’s specifically crafted to make a point. But there’s a long and complex history behind that little difference.

I saved both files in a different encoding, Mac.txt in Western (Mac OS Roman), Win.txt in Western (Windows Latin 1). In the first case, the character ß is located in position DF of the code page. In the latter, it’s in A7 instead.

If that all sounds strange and unfamiliar to you, and you never heard the term code page, enjoy the fact that you are living in 2023! Typically, we no longer have to deal with gory details of data storage for text. Historically, when storage space was highly constrained, this used to be a major nightmare in computing. Getting a simple plain text file from a Windows computer to a Mac typically meant you needed to know such details and convert the format along the way, or you would just receive garbage on the other end. If you are interested in learning more about text and encodings, watch this terrific talk titled There’s No Such Thing As Plain Text.

Kaleidoscope focuses on content, so it doesn’t bother users with details like different text encoding. But if you need to investigate details, now you can. Read on…

Kaleidoscope and comparing binary data

Using Shortcuts (and the Finder)

As we have done before on several occasions, we’re going to employ macOS Shortcuts to create a solution for comparing binary data of files in Kaleidoscope, without the need to change Kaleidoscope itself.

Shortcuts is an automation technology on macOS (and other Apple platforms), to be used with an app by the same name. To clarify the naming mess, a thing you make in Shortcuts is called a shortcut (small s), which itself consists of actions (or shortcut actions). Kaleidoscope supplies several actions to Shortcuts. This means that data coming out of other actions can be sent into Kaleidoscope for doing comparisons.

The beauty is that these individual shortcuts can be used directly from the Finder, or the menu bar, to act on a selection of files.

In 2018, Till Toenshoff created a workflow based on Automator to compare binary data using Kaleidoscope, and posted it to GitHub. Our solution is inspired by his work.

The only real processing that our shortcut needs to do is run each file through a tool called xxd, which transforms the binary data into readable hexadecimal characters.

Here’s a screenshot of the completed shortcut:

The Shortcuts app with our completed shortcut open. The core part is the Run Shell Script action that executes xxd.
The Quick Actions section of the Finder contextual menu after installing and configuring the shortcut.

Follow the instructions in the sidebar to download and install the shortcut, in case you are not yet familiar with the process. Once set up, you can select a number of files of any kind in Finder, bring up the contextual menu, and select Quick Actions > Compare Binary Files.

Doing so will process those files using the shortcut and compare their binary representations in Kaleidoscope. This can be helpful in certain circumstances, see above. Note that you are not limited to two files. Use the File Shelf in Kaleidoscope to compare various combinations.

Using the command line

Some Kaleidoscope users prefer dealing with the command line. Almost the same functionality that took us several setup steps in Shortcuts can be achieved using an elegant single line command in Terminal:

ksdiff <(xxd A.txt) <(xxd B.txt)

Refer to the xxd man page to fine-tune the layout to your needs. 🙂

Other formats

Do you need to compare files that can’t be read by Kaleidoscope? Maybe there’s a way to automate the process in a tool like Shortcuts. Let us know. We are always interested to learn from our users…

Kaleidoscope

Take Kaleidoscope for a spin

Download Kaleidoscope now and use it free of charge for 7 days.
If you have questions along the way, contact us and we will be happy to assist!

Download TrialmacOS Sequoia (15.x) or later

Monthly and yearly plans are available, starting at $8/month.
Upgrade discount available for existing customers.

Subscribe NowSee Pricing and Subscribe