我如何像UNIX中的Hexdump函数一样打印出地址
我有一个作业,我想在其中模拟 unix 的 hexdump 函数。我的程序将文件转储为十六进制,但在打印偏移量和可打印字符时遇到问题。
这是我的代码:
/*performs a hex and an octal dump of a file*/
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define LINESIZE 512
void hexdump(FILE *fp);
void octaldump(FILE *fp);
int main(int argc, char * argv[])
{
FILE *fp;
if((fp = fopen(argv[1], "rb+")) == '\0')
{
perror("fopen");
return 0;
}
hexdump(fp);
return 0;
}
void hexdump(FILE *fp)
{
char temp [LINESIZE];
size_t i = 0;
size_t linecount = 1;
long int address = 0;
while(fscanf(fp," %[^\n] ", temp) == 1)
{
printf("%5d", address);
for(i = 0; i < strlen(temp); i++)
{
printf("%02x ", temp[i]);
if(linecount == 16)
{
printf("\n");
linecount = 0;
}
linecount++;
}
printf(" | ");
for(i = 0; i < strlen(temp); i++)
{
if(temp[i] < 32)
{
printf(".");
}
else
{
printf("%c", temp[i]);
}
}
printf("\n");
}
}
正如我上面所说,我的程序应该打印偏移量,然后打印用 0 填充的文件的十六进制值,然后打印文件的可打印字符,如下所示。
0000000 7f 2a 34 f3 21 00 00 00 00 00 00 00 00 00 00 00 | test file
我设法打印出可打印字符,但它们出现在十六进制部分之后。因此,如果十六进制代码的一行延伸到下一行,它将在下一行打印可打印字符。
例如:
2a 3b 4d 3e 5f
12 43 23 43 | asfdg df
我如何让它打印一行十六进制字符之后出现的任何字符?
PS:由于某种原因,我的程序不会填充 0。
EDIT1:我得到了偏移量部分,我只是继续将 16 添加到我的地址变量中并继续打印
There I have this assignment where I am suppose to emulate the hexdump function from unix. I got my program to dump files into hex but i am having trouble printing the offset and the printable characters.
Here is my code:
/*performs a hex and an octal dump of a file*/
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define LINESIZE 512
void hexdump(FILE *fp);
void octaldump(FILE *fp);
int main(int argc, char * argv[])
{
FILE *fp;
if((fp = fopen(argv[1], "rb+")) == '\0')
{
perror("fopen");
return 0;
}
hexdump(fp);
return 0;
}
void hexdump(FILE *fp)
{
char temp [LINESIZE];
size_t i = 0;
size_t linecount = 1;
long int address = 0;
while(fscanf(fp," %[^\n] ", temp) == 1)
{
printf("%5d", address);
for(i = 0; i < strlen(temp); i++)
{
printf("%02x ", temp[i]);
if(linecount == 16)
{
printf("\n");
linecount = 0;
}
linecount++;
}
printf(" | ");
for(i = 0; i < strlen(temp); i++)
{
if(temp[i] < 32)
{
printf(".");
}
else
{
printf("%c", temp[i]);
}
}
printf("\n");
}
}
As i said above, my program is suppose to print the offset then the hex value of the file padded with 0's, then the printable characters of the file like so.
0000000 7f 2a 34 f3 21 00 00 00 00 00 00 00 00 00 00 00 | test file
I managed to print out the printable characters, but they appear after the hex part. So if one line of hexcode extends to the next line, it prints the printable characters on the nextline.
For example:
2a 3b 4d 3e 5f
12 43 23 43 | asfdg df
How do i get it to print whatever character appears after one line of the hex characters?
PS: For some reason my program doesn't pad 0's for some reason.
EDIT1: I got the offset part, i just keep adding 16 to my address variable and keep printing
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于这是家庭作业,我只会为您提供一些如何解决问题的指导。我会尽量含糊其辞,以免影响你的学习。
首先,不要使用
fscanf
如果您正在编写十六进制转储程序,请使用fread
代替。scanf
系列函数适用于文本,而十六进制转储器通常用于二进制数据。如果您使用
fread
并一次读取足够的字节来填充一行输出,那么您可以使用两个for
循环生成所需的输出(一个用于十六进制,一个用于十六进制)对于字符)后跟一个换行符。至于您的零填充问题,请阅读 printf手册页,您的
"%5d"
格式字符串需要多一点。Since this is homework, I'll just give you some pointers on how to approach your problem. I'll be as vague as I can so as to not interfere with your learning.
First of all, don't use
fscanf
if you're writing a hex dumper, usefread
instead. Thescanf
family of functions are meant for text and hex dumpers are usually used with binary data.If you use
fread
and read just enough bytes at a time to fill one line of output, then you can produce your desired output with twofor
loops (one for hex, one for characters) followed by a single newline.As far as your zero padding problem goes, read the
printf
man page, your"%5d"
format string needs a little bit more.