如何在 macOS 比對兩個二進位檔案(Hex Diff 完整教學)
二進位比對(Binary Diff)是在 byte 層級找出兩個檔案之間每一個差異位元組的技術。不同於文字 diff 只比對可讀行,binary diff 能處理任何格式——執行檔、韌體 blob、編譯輸出、圖片、資料庫檔、封包截圖——只要是二進位格式,它都能告訴你第幾個 offset 的 byte 從什麼值變成什麼值。
為什麼需要 Binary Diff?
在日常開發與維運中,以下幾個情境一定會讓你需要用到 binary diff:
韌體更新驗證:下載新版韌體後,你想確認更新了哪些區段、哪些位置的值改變了,而不是盲目信任版本號。比對新舊兩個 .bin 或 .img,可以立刻找到差異 offset,搭配手冊確認改動是預期的 bug fix 還是意外的 regression。
Build 重現性確認(Reproducible Builds):Rust、C++、Go 在不同時間或不同機器 compile 出來的 binary 理論上應該是 bit-for-bit 相同的,但時間戳記嵌入、連結器順序、路徑字串都可能導致微小差異。用 binary diff 可以快速找出是哪個 section(.rodata、.debug_info)引入了不一致。
惡意程式分析:安全研究員比對正常執行檔和被竄改版本,找出植入程式碼的起始 offset,是 binary diff 最經典的使用場景之一。即使你不是資安研究員,確認下載的二進位和官方 checksum 一致也是好習慣的延伸。
影像與資產驗證:PNG、JPEG、PDF、資料庫 .db 檔——同名檔案在不同系統同步後,用肉眼看不出差異,但 binary diff 能告訴你有沒有 byte 被修改,適合驗證備份完整性。
Compiled Protobuf / MessagePack 資料:序列化格式的二進位輸出很難直接閱讀,比對兩個版本的序列化結果時,binary diff 搭配 offset 資訊能快速縮小問題範圍。
工具對照表
| 工具 |
GUI |
Byte 級 Diff |
Offset 顯示 |
Hex 搜尋 |
免費 |
Apple Silicon |
| Lode Binary Diff |
✅ |
✅ 三色高亮 |
✅ |
✅ |
✅ |
✅ 原生 |
xxd + diff(Terminal) |
❌ 純文字 |
✅(間接) |
✅ |
❌ |
✅ macOS 內建 |
✅ |
hexdump -C + diff |
❌ 純文字 |
✅(間接) |
✅ |
❌ |
✅ macOS 內建 |
✅ |
| HexFiend |
✅ |
✅ 並列 |
✅ |
✅ |
✅ |
✅ |
| FileMerge(Xcode) |
✅ |
❌ 無法處理 |
❌ |
❌ |
✅ |
✅ |
簡短選購建議:想要最直觀的 GUI + 顏色高亮,用 Lode;想要無需安裝的 Terminal 方案,用 xxd + diff;想要功能完整的免費 hex editor,用 HexFiend;FileMerge 不適合 binary diff,遇到二進位檔案會直接報錯或顯示亂碼。
方法一:Lode Binary Diff(推薦)
Lode 是原生 macOS 工具(Tauri 2 + Rust),整合了六種工作模式——包含 Binary Compare 與 Hex View——在同一個 App 內。Binary Diff 模式採用雙欄並列設計,左右各顯示一個檔案的十六進位值與 ASCII 表示,差異位置即時高亮,不需要任何前置轉換。
操作步驟
步驟一:開啟 Lode,選擇 Binary Diff 模式
從 Dock 點擊 Lode 圖示,或用 Spotlight(⌘Space)搜尋「Lode」開啟。啟動後在頂端的模式選擇列點擊「Binary Diff」(有些版本顯示為「Hex View」)。
步驟二:載入兩個檔案
將第一個二進位檔案拖進左側面板,第二個拖進右側面板。也可以透過選單 File → Open 分別選取。Lode 支援任意大小的二進位檔案,後端用 Rust 非同步讀取,大型 firmware blob 或資料庫檔都不會卡住 UI。
步驟三:讀懂 Hex Diff 輸出
Lode 以十六進位(hex)搭配右側 ASCII 解碼的雙欄格式顯示兩個檔案。顏色語義如下:
| 顏色 |
意義 |
| 🟠 橙色 |
同位置 offset 的 byte 值不同(修改) |
| 🟢 綠色 |
右側新增的 byte 區段(插入) |
| 🔴 紅色 |
左側多出、右側缺少的 byte 區段(刪除) |
| 白色 |
完全相同 |
每行的最左側欄位是十六進位 offset,例如 0x000012A0 代表從檔案開頭算起第 4768 個 byte。這個 offset 是你回報 bug 或撰寫 patch 時需要的精確位置。
步驟四:在差異之間快速導覽
視窗右側有差異導覽按鈕(向上 / 向下箭頭),或按 ⌘↓ 跳到下一處差異、⌘↑ 跳回上一處。對於有數百個差異的大型 binary,這個功能省下大量手動捲動時間。
步驟五:複製 Hex 值或 Offset 範圍
在任何高亮區域上按右鍵,可以複製選取範圍的十六進位值、十進位 offset 範圍、或 ASCII 解碼字串,方便直接貼入 bug report、patch note 或 Slack 訊息。
為什麼 Lode 的 Binary Diff 好用
文字 diff 工具(包括 FileMerge)遇到二進位檔案時,通常要嘛報錯,要嘛把整個檔案當成一行亂碼輸出。Lode 的 Binary Compare 模組是為 byte-level 比對而設計:行對齊採用 LCS(最長公共子序列)算法,確保即使兩個檔案長度不同、插入了新的 byte 區段,對齊仍然正確,不會因為一個 byte 的插入導致後面所有行都顯示為「不同」。
方法二:Terminal — xxd + diff
macOS 內建的 xxd 指令可以把任何 binary 轉成可讀的 hex dump 文字,再用 diff 比對兩個 hex dump,就能找出差異位置。這個方法不需要安裝任何東西,只要有 Terminal 就能用。
xxd /path/to/file1.bin > /tmp/a.hex
xxd /path/to/file2.bin > /tmp/b.hex
diff /tmp/a.hex /tmp/b.hex
xxd 的輸出格式每行顯示 16 bytes,格式是 offset: hex_values ascii_representation。例如:
00001200: 4865 6c6c 6f20 576f 726c 6400 0000 0000 Hello World.....
diff 輸出會標示哪幾行有差異(< 代表 file1,> 代表 file2)。你可以從行首的 offset 直接換算出差異在原始 binary 的哪個位置。
限制
- 輸出是純文字,沒有顏色,很難快速掃視大量差異。
- 如果兩個 binary 之間有「插入」(一個 binary 在某個位置多了幾個 byte),
diff 會把後面所有行都標記成「不同」,而不是把插入位置對齊,視覺上非常混亂。
- 大型檔案(> 10 MB)的
xxd 轉換需要幾秒鐘,臨時檔案也會占用同等大小的磁碟空間。
- 搜尋特定 hex 值需要另外配合
grep,不如 GUI 工具直觀。
如果只需要快速確認兩個小型 binary 是否完全相同(而不需要知道具體哪裡不同),cmp -l file1 file2 或 md5 file1 && md5 file2 更快。
方法三:hexdump -C
hexdump -C 是 xxd 的替代方案,輸出格式稍有不同:
hexdump -C /path/to/file1.bin > /tmp/a.hex
hexdump -C /path/to/file2.bin > /tmp/b.hex
diff /tmp/a.hex /tmp/b.hex
-C 旗標同樣以十六進位 + ASCII 雙欄格式顯示,每行 16 bytes,行首是 offset。用法和 xxd + diff 幾乎相同,選哪個主要看個人習慣。xxd 的輸出格式在需要搭配 xxd -r 反轉回 binary 時更方便;hexdump -C 的格式在某些 Unix 系統上更普及。
方法四:HexFiend
HexFiend 是 macOS 上最知名的免費開源 hex editor,原生支援 Apple Silicon,可以開啟幾 GB 的大型 binary 而不佔用等量記憶體(streaming 讀取)。
HexFiend 提供「Compare Files」功能(Tools → Compare Files),可以並列顯示兩個檔案的 hex 並高亮差異,操作體驗接近 Lode 的 Binary Diff。如果你的主要需求是 hex 編輯(修改特定 offset 的值),HexFiend 是比 Lode 更合適的工具——Lode 的 Binary Diff 是唯讀比對,不提供編輯功能。
何時選 HexFiend vs Lode
- 需要編輯 binary → HexFiend(hex editor)
- 需要 binary diff 搭配資料夾比對、全文搜尋整合在同一個工作台 → Lode
- 只需要 diff,不需要其他功能 → 兩者都可以,看個人偏好
Rex 的實際使用經驗
在開發 Lode 的過程中,我需要頻繁確認 Rust 編譯出來的二進位在不同 build 之間有沒有意外的差異。理論上 deterministic build 應該 bit-for-bit 相同,但我遇到過幾次 .debug_info section 因為路徑字串嵌入導致 hash 不一致的問題。
用 Lode 的 Binary Diff 比對兩個 .app/Contents/MacOS/lode 執行檔,可以在幾秒內找到差異集中在哪個 offset 區間,再搭配 nm 或 dwarfdump 定位到具體的 symbol。相比之前用 xxd + diff,最大的差別是:xxd 方法在有「插入」的情況下對齊會亂掉,Lode 的 LCS 算法對齊是正確的,不會因為幾個 byte 的插入讓後面幾百行都顯示紅色。
另一個用到 binary diff 的情境是比對 SSD 韌體的 firmware blob。把新版和上一版的 .bin 拖進 Lode,一眼就能看出哪些 section 有改動,哪些是完全相同的——這個資訊在追蹤特定 regression 是哪個版本引入時非常有用,省去從 changelog 反推的時間。
常見問題
Q:如何在 macOS Terminal 比對兩個 binary 檔案?
最簡單的方法:xxd file1 > /tmp/a.hex && xxd file2 > /tmp/b.hex && diff /tmp/a.hex /tmp/b.hex。這會把兩個 binary 轉成 hex dump 文字再比對。如果只需要確認是否完全相同:cmp file1 file2(無輸出 = 完全相同)。
Q:Mac 上有免費的 Hex Diff 工具嗎?
有兩個主要選擇:Lode(整合式工作台,含 Binary Diff)和 HexFiend(專門的 hex editor,含 Compare Files 功能)。兩者都是免費的原生 macOS 工具,都支援 Apple Silicon。
Q:Binary Diff 和 Text Diff 有什麼差別?
Text diff 以「行」為單位比對,遇到二進位檔案會把無法顯示的 byte 當作亂碼,通常輸出 Binary files differ 就停止。Binary diff 以「byte」為單位比對,精確顯示每個不同 byte 的 offset 和值,適用於任何非文字格式的檔案。
Q:Lode 能開啟大型 binary 檔案嗎?
可以。Lode 的 binary 讀取後端用 Rust 非同步 I/O 實作,不會把整個檔案一次載入記憶體。幾百 MB 的 firmware blob 或資料庫 .db 檔都能正常開啟和比對,UI 不會因為讀取而卡頓。
How to Compare Binary Files on Mac — Complete Hex Diff Guide 2026
Binary comparison works at the byte level: instead of comparing lines of text, it finds every single byte that differs between two files, showing you the exact offset, the old value, and the new value. Unlike text diff, binary diff works on any file format — executables, firmware blobs, compiled output, images, databases, serialized data — as long as it has bytes, binary diff can tell you what changed.
Why Binary Comparison Matters
Several day-to-day scenarios make binary diff an essential tool:
Firmware update verification: When you download a new firmware version, you want to know exactly which byte regions changed — not just trust the version number. Comparing old and new .bin or .img files at the byte level lets you verify that changes are in the expected sections and nothing unexpected was modified.
Reproducible build checking: Rust, C++, and Go builds should ideally be bit-for-bit identical across machines and time. But embedded timestamps, linker ordering, and path strings can introduce subtle differences. Binary diff quickly identifies which ELF section (.rodata, .debug_info) is introducing the inconsistency.
Malware and security analysis: Security researchers compare clean executables against modified versions to find injected code by offset. Even for regular developers, verifying a downloaded binary against an official checksum is good practice — and binary diff is the tool that tells you where they differ if they don’t match.
Asset and backup integrity: PNG, JPEG, PDF, SQLite .db files can look identical visually but have byte-level differences after sync or transfer. Binary diff gives you ground truth about whether a backup is truly identical to the original.
Compiled Protobuf / binary serialization formats: When two versions of a serialized binary payload differ, binary diff narrows down which byte region is the source of the discrepancy, saving significant debugging time.
| Tool |
GUI |
Byte-level Diff |
Offset Display |
Hex Search |
Free |
Apple Silicon |
| Lode Binary Diff |
✅ |
✅ 3-color highlight |
✅ |
✅ |
✅ |
✅ Native |
xxd + diff (Terminal) |
❌ Text only |
✅ (indirect) |
✅ |
❌ |
✅ Built-in |
✅ |
hexdump -C + diff |
❌ Text only |
✅ (indirect) |
✅ |
❌ |
✅ Built-in |
✅ |
| HexFiend |
✅ |
✅ Side-by-side |
✅ |
✅ |
✅ |
✅ |
| FileMerge (Xcode) |
✅ |
❌ Not binary-aware |
❌ |
❌ |
✅ |
✅ |
Quick recommendation: for the most intuitive GUI with color-coded diff, use Lode; for a no-install Terminal solution, use xxd + diff; for a fully-featured free hex editor, use HexFiend. FileMerge is not suitable for binary files — it will error out or display garbled output.
Method 1: Lode Binary Diff (Recommended)
Lode is a native macOS application (Tauri 2 + Rust) that integrates six work modes — including Binary Compare and Hex View — in a single app. The Binary Diff mode uses a side-by-side two-column layout, showing each file’s hexadecimal values alongside their ASCII representation, with differences highlighted in real time. No conversion step required.
Step-by-Step Walkthrough
Step 1: Open Lode and select Binary Diff mode
Launch Lode from your Dock or use Spotlight (⌘Space, type “Lode”). In the mode selector at the top, click Binary Diff (some builds label it Hex View).
Step 2: Load the two binary files
Drag the first binary file onto the left panel and the second onto the right panel. Alternatively, use File → Open to select each file. Lode handles files of any size through Rust async I/O — large firmware blobs or database files load without freezing the UI.
Step 3: Read the hex diff output
Lode displays each file in the standard hex editor format: a hex offset column on the far left, 16 bytes per row in hex, and an ASCII representation on the right. Differences are highlighted with three distinct colors:
| Color |
Meaning |
| Orange |
Same offset, different byte value (modified) |
| Green |
Bytes present only in the right file (inserted) |
| Red |
Bytes present only in the left file (deleted) |
| White |
Identical bytes |
The offset shown (e.g., 0x000012A0) tells you the exact byte position from the start of the file — the number you need for a bug report or patch note.
Step 4: Navigate between differences
The right sidebar shows diff navigation arrows. Use ⌘↓ to jump to the next changed region and ⌘↑ to go back to the previous one. For large binaries with hundreds of differences, this navigation saves significant manual scrolling.
Step 5: Export or copy hex values
Right-click any highlighted region to copy the hex values, decimal offset range, or ASCII-decoded string. This makes it easy to paste exact byte positions into a bug report, Slack message, or patch note.
Why Lode’s alignment is correct
Most text diff tools, when forced onto binary data, produce misleading output: a single inserted byte causes every subsequent row to appear as “different” because the line boundaries shift. Lode’s Binary Compare uses an LCS (Longest Common Subsequence) algorithm to align the two files before highlighting, so an insertion is shown as an insertion rather than corrupting the alignment of everything after it.
Method 2: Terminal — xxd + diff
macOS ships with xxd, a utility that converts any binary file into a readable hex dump. Combining it with diff gives you a basic binary comparison without installing anything:
xxd /path/to/file1.bin > /tmp/a.hex
xxd /path/to/file2.bin > /tmp/b.hex
diff /tmp/a.hex /tmp/b.hex
xxd output looks like this:
00001200: 4865 6c6c 6f20 576f 726c 6400 0000 0000 Hello World.....
Each row shows 16 bytes: the left column is the hex offset, the middle is the hex values (grouped in pairs), and the right is the ASCII representation (non-printable bytes shown as .).
diff output marks lines with < (from file1) and > (from file2). You can read the offset from the start of each row.
Limitations of the Terminal approach
- No color: output is monochrome text, hard to scan visually for large diffs.
- Insertion alignment breaks: if file2 has extra bytes inserted relative to file1,
diff marks everything after the insertion as different, even if it’s actually the same content shifted by a few bytes.
- Conversion overhead:
xxd writes a temporary text file roughly 3x the size of the original binary.
- No interactive search: finding a specific hex pattern requires piping through
grep.
For a quick existence check (same or different, no detail needed), cmp file1 file2 is faster — it exits with no output if files are identical, or reports the first differing byte position.
Method 3: hexdump -C
hexdump -C is an alternative to xxd with a slightly different output format:
hexdump -C /path/to/file1.bin > /tmp/a.hex
hexdump -C /path/to/file2.bin > /tmp/b.hex
diff /tmp/a.hex /tmp/b.hex
The -C flag produces the canonical hex+ASCII format: 16 bytes per line, hex offset at left, ASCII at right. Usage and limitations are essentially the same as xxd + diff. The main practical difference is that xxd supports a round-trip back to binary (xxd -r), which is useful if you need to patch specific bytes. Choose based on your preference or the tooling available on your target system.
Method 4: HexFiend
HexFiend is the leading free, open-source hex editor for macOS, with native Apple Silicon support. It uses streaming reads to handle multi-gigabyte files without loading the entire content into memory.
HexFiend’s Tools → Compare Files feature provides a side-by-side hex view with difference highlighting — similar in concept to Lode’s Binary Diff. If your primary need is hex editing (changing specific byte values), HexFiend is the right tool: Lode’s Binary Diff is read-only comparison, not an editor.
HexFiend vs Lode: when to pick which
- Need to edit bytes at specific offsets → HexFiend
- Need binary diff integrated with folder compare and full-text search in one workspace → Lode
- Need only binary diff with no other features → both work; personal preference
Rex’s Personal Experience
During Lode development, I frequently needed to verify that Rust-compiled binaries were deterministic across builds. The theory is that deterministic builds produce bit-for-bit identical output — in practice, I ran into cases where .debug_info sections embedded absolute build-path strings, causing the binary to differ between machines even when the source was identical.
Using Lode’s Binary Diff to compare two builds of lode (the macOS executable inside the .app bundle) immediately showed the differing byte region, which I could then cross-reference with nm or dwarfdump to identify the specific debug symbol. The key advantage over xxd + diff was alignment: the path string differences were surrounded by identical bytes, and xxd + diff kept breaking alignment at every insertion point, making the output nearly unreadable. Lode’s LCS-based alignment kept the surrounding identical content in sync, so only the actually-changed bytes were highlighted.
I also use binary diff when working with firmware blobs — dragging old and new versions of a .bin file into Lode’s Binary Diff takes a few seconds to parse, then gives me a clear visual map of which byte regions changed and which are untouched. This is much faster than reading changelogs to figure out which firmware version introduced a specific behavior change.
Frequently Asked Questions
Q: How do I compare two binary files in macOS Terminal?
Use xxd to convert both files to hex dumps, then diff them: xxd file1 > /tmp/a.hex && xxd file2 > /tmp/b.hex && diff /tmp/a.hex /tmp/b.hex. For a simple same/different check without detail, use cmp file1 file2 — no output means identical.
Q: Is there a free hex diff tool for Mac?
Yes — two good ones. Lode provides an integrated Binary Diff mode alongside folder compare and full-text search. HexFiend is a dedicated hex editor with a Compare Files feature. Both are free, open source, and native on Apple Silicon.
Q: What’s the difference between binary diff and text diff?
Text diff operates on lines of text — it will output Binary files differ and stop when it encounters non-text content. Binary diff operates on individual bytes, reporting the exact offset and value change for every byte that differs, regardless of file format. Binary diff works on executables, images, databases, and any other non-text file.
Q: Can Lode open large binary files?
Yes. Lode’s binary read backend uses Rust async I/O with streaming reads, so it does not load the entire file into memory at once. Multi-hundred-megabyte firmware blobs or large SQLite database files open and diff without blocking the UI.