# C 语言编写二进制/十六进制编辑器

基础知识

计算机中表示信息的最小单位是 bit(位),而用来存储数据的最小单位是 Byte(字节)。因为存储数据的最小单位是字节,所以文件长度不存在 4bit 或者 1bit 这种表示。

编写十六进制编辑器

数据可视化

每次从文件读取 8bit 数据存储在 unsigned char 类型中,用 printf() 的 %x 参数打印;如果你想打印数据的二进制形式,只需对 unsigned char 类型数据进行八、十或十六进制转二进制字符串在打印即可。

数据编辑

表面上我们的数据存储在 unsigned char 类型中,编辑的最小单位因该是 unsigned char,如果我们想修改其中任意一位(bit),该如何操作呢?我们举个例子:

二进制 0000 0000 的高位设置为 1 该怎么办? 二进制 0000 0000 的十六进制表示为 0; 二进制 1000 0000 的十六进制表示为 80; 所以只要将十六进制 80 存入文件,便高位设置为了 1。

解析文件差异对比工具

文件存储的最小单位是字节(Byte),不存在 4bit 或者 1bit 这种表示。我们可以每次从文件读取 8bit 数据存储在 unsigned char 类型中,然后再相互比较两个数据即可。

输入图片说明

二进制数据用十六进制的方式表示,是因为十六进制最大值 F 刚好存储在 4bit 中,两个十六进制数就占用 1Byte 的空间。直观方便易于查阅和修改。旁边的 ASCII 字符区更多是用来辅助阅览和编辑的。


我创建了两个文件,分别各写入一个 int 值,下图是文件差异:

输入图片说明

两个 int 的值是完全不同的,但他们的差异仅仅只有 1Byte 而已。也就是说这两个文件在磁盘中实际上只有一个字节不同而已。


输入图片说明

我向 b.bin 写入的数据。(a.bin 写入的是变量 a)

输入图片说明

b.c 文件是 HxD 导出的二进制数据。

输入图片说明

输入图片说明

这是将 unsigned char rawData[] 写入文件(c.bin)后再读取的结果。 也就是说所谓类型对计算机而言是不存在的。

你可能注意到 HxD 的结果为 00 FF FF FF 我的结果为 FF FF FF 00,原因在于我打印的是 int 型数据的十六进制形式。

转载于:https://my.oschina.net/bbi094/blog/703283

我用它来修改ldf,你呢? XVI32 is a free hex-editor with the following main features: - New: Runs on systems with > 2 GB virtual memory - New: Bugfix (script command CHARCON is now working) - Simplified search for Unicode Latin (UTF-16) strings - Script commands BITNOT, BITAND, BITOR, BITXOR, BITSHL, BITSHR - Reload command - Wheel mouse support - Computing of CRC16 (standard) and CRC32 (PKZIP compatible) for complete file and selected block (only if block is currently selected) - Data inspector to view decoded numbers - Built-in script interpreter! Refer to online help for details - Easily works with huge files. Try to open a 60 MB sized text file with some other hex editor (not to speak about Wordpad), then use XVI32... - XVI32 allows to edit files up to 2 GB (enough virtual memory provided, of course) - For your convenience, XVI32 stores settings and last used search strings etc. in XVI32.INI file - Progress indication in percent for most operations - You can abort nearly all operations (reading/writing files, search, replace, print...) - Display of both text (ASCII/ANSI) and hexadecimal representation - Two synchronous cursors in text and hex area - Fully resizeable window (change number of rows and columns) - Font and font size adjustable - Overwrite or insert characters - Insert text or hex string n times - Switch byte offset (address) of first byte between 0 or 1 to examine also record structure of plain text files - Search text or hex string, e.g. find "this text" or find "0D 0A" - Search optionally with joker char, e.g. find "A.C" or "00 2E 2E 00" where "." = "2E" (user-defined) stands for any character - Fast searching algorithm (Quicksearch) for both search directions (down/up) - Count occurences of text or hex string - Replace text or hex string, e.g. replace "0D 0A" by "0A" or replace "0D 0A" by text "EOL" - Extremely fast "replace all" mode (if needed, additional memory is allocated beforehead, not at every single replacing operation) - Auto-fill feature to copy bytes from current address into input field for hex string using right arrow key - Character conversion using self-defined character table - Easy converting of text to hex string in dialogs (e.g. "abc" -> "61 62 63") - Decoding and encoding of 1, 2, 4, and 8 byte integers or 4/8 byte floats in 2 possible byte orders - Bit manipulation (view or set bits) - Open file in Read Only mode (e.g. if opened by another application or to avoid unintentional modifications) - Insert file contents into file - Write block to file - Copy, move or delete block - Clipboard support - Goto address (absolute or relative up/down) - Up to 9 named bookmarks - Enter jump width and jump up/down (useful for files with fixed record length) - Patch BORLAND PASCAL 7.0 EXE files for execution on processors > 200 MHz - Printing with preview or print to file - Easily access most recently used files - No setup programm needed, doesn't write any data to registry - And last, but not least: XVI32 is free!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值