diff --git a/docs/zh/technical/Basic-knowlegde/raw-data-view.md b/docs/zh/technical/Basic-knowlegde/raw-data-view.md new file mode 100644 index 0000000..41c627d --- /dev/null +++ b/docs/zh/technical/Basic-knowlegde/raw-data-view.md @@ -0,0 +1,208 @@ +# Viewing Raw Data in Hex or Binary on macOS / Linux / Windows (CLI Guide) + +Inspecting raw bytes is essential for debugging, reverse-engineering, verifying file formats, or examining file headers. +This guide introduces common command-line tools to display file contents in **hexadecimal** or **binary**, and how to limit output to a specific number of bytes. + +--- + +## macOS & Linux + +macOS and Linux share most Unix tools, so their usage is nearly identical. + +--- + +## 1. `hexdump` + +### Hex output + +```sh +hexdump -C file.bin +``` + +* `-C` → canonical hex + ASCII view (similar to `xxd`) + +### Limit output to N bytes + +#### First 16 bytes + +```sh +hexdump -C -n 16 file.bin +``` + +#### 16 bytes from offset 32 + +```sh +hexdump -C -s 32 -n 16 file.bin +``` + +* `-s OFFSET` → seek to offset +* `-n BYTES` → read N bytes + +--- + +## 2. `xxd` + +### Hex output + +```sh +xxd file.bin +``` + +### Binary output + +```sh +xxd -b file.bin +``` + +### Limit output to N bytes + +#### First 16 bytes + +```sh +xxd -l 16 file.bin +``` + +#### 16 bytes from offset 32 + +```sh +xxd -s 32 -l 16 file.bin +``` + +#### First 16 bytes in binary + +```sh +xxd -b -l 16 file.bin +``` + +--- + +## 3. `od` (POSIX standard) + +### Hex output + +```sh +od -Ax -t x1 file.bin +``` + +### Binary output + +```sh +od -An -t b1 file.bin +``` + +### Limit output to N bytes (hex) + +```sh +od -Ax -t x1 -N 16 file.bin +``` + +### Limit output to N bytes (binary) + +```sh +od -An -t b1 -N 16 file.bin +``` + +--- + +## 4. `hd` + +Available on macOS via Homebrew (`brew install hexedit`) or on Linux (util-linux). + +```sh +hd file.bin +``` + +--- + +# Windows + +--- + +## 1. PowerShell: `Format-Hex` (built-in) + +### Hex output + +```powershell +Format-Hex file.bin +``` + +### Limit output to N bytes + +#### First 16 bytes (hex) + +```powershell +Get-Content file.bin -Encoding Byte -TotalCount 16 | Format-Hex +``` + +#### First 16 bytes (binary) + +```powershell +Get-Content file.bin -Encoding Byte -TotalCount 16 | ForEach-Object { + [Convert]::ToString($_, 2).PadLeft(8, '0') +} +``` + +--- + +## 2. CertUtil (hex only) + +```cmd +certutil -dump file.bin +``` + +CertUtil cannot directly limit byte count, but you can limit lines using a Unix-like environment (Git Bash/MSYS2/WSL): + +```sh +certutil -dump file.bin | head -n 5 +``` + +--- + +## 3. Windows Subsystem for Linux (WSL) + +WSL allows you to use all Linux tools: + +```sh +xxd -l 16 file.bin +``` + +--- + +# Summary Table + +| Platform | Hex Output | Binary Output | Limit Output Size | +| -------- | ------------------------------- | ------------------------- | ------------------------------- | +| macOS | `xxd`, `hexdump -C`, `od -t x1` | `xxd -b`, `od -t b1` | `hexdump -n`, `xxd -l`, `od -N` | +| Linux | same as macOS | same as macOS | same as macOS | +| Windows | `Format-Hex`, `certutil -dump` | PowerShell bit conversion | `Get-Content -TotalCount` | +| WSL | Linux tools | Linux tools | Linux tools | + +--- + +# Quick Examples + +### Print first 32 bytes in hex + +```sh +xxd -l 32 file.bin +``` + +### Print bytes starting at offset 0x100 + +```sh +hexdump -C -s 0x100 -n 64 file.bin +``` + +### Print first 10 bytes in binary (Linux/macOS) + +```sh +od -An -t b1 -N 10 file.bin +``` + +### Print first 16 bytes in binary (Windows PowerShell) + +```powershell +Get-Content file.bin -Encoding Byte -TotalCount 16 | % { [Convert]::ToString($_,2).PadLeft(8,'0') } +``` + +> Generated By ChatGPT-5.1