从 C 语言中机器代码形式的文本文件中读取字节?
我有一个带有以下形式的机器代码的文本文件:
B2 0A 05
B2 1A 01
B3 08 00 17
B2 09 18
其中指令具有以下格式:
OP 模式操作数
注意:操作数可以是 1 或 2 个字节。
其中:(示例)
OP = B2
模式 = 0A
Operand = 05
如何读取变量中的字节?如上例所示。 当我阅读该文件时,我得到了单个字符。我有一个指针数组,我在其中读取单独的行,但仍然无法解决读取字节的问题。
任何想法、建议。
我希望我没有让这里的任何人感到困惑。
谢谢。
I have a text file with machine code in this form:
B2 0A 05
B2 1A 01
B3 08 00 17
B2 09 18
where an instruction has this format:
OP Mode Operand
Note: Operand could be 1 or 2 bytes.
Where:(example)
OP = B2
Mode = 0A
Operand = 05
How can I read the bytes in a variable? As shown in the above example.
When i read the file I get individual characters. I have an array of pointers where I read individual line, but still cannot solve the problem of reading a byte.
Any ideas,suggestions.
I hope I am not confusing anyone here.
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
考虑使用
fscanf
。您可以使用%x
格式说明符来读取十六进制整数。Consider using
fscanf
. You can use the%x
format specifier to read hexadecimal integers.验证文件是否以二进制模式(“rb”)打开。
使用
fread
一次读取一个字节:一种更有效的方法是将块或块数据读入缓冲区并解析使用指向 const unsigned char 的指针进行缓冲区:
更安全的设计是使用函数 Get_Byte(),该函数返回下一个数据字节(并在必要时重新加载缓冲区)。
Verify that the file is opened in binary mode ("rb").
Use
fread
to read one byte at a time:A more efficient method is to read in chunks or blocks of data into a buffer and parse the buffer using a pointer to
const unsigned char
:A safer design is to use a function,
Get_Byte()
, which returns the next data byte (and reloads buffers if necessary).