问题打印十六进制 [英] Problem printing Hexadecimal

查看:100
本文介绍了问题打印十六进制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在尝试读取一个exe文件,并用十六进制格式的

字符打印出来。

hexadecimal

0x4d 0x5a 0x90 0x00 0x03 ....等等这个文件是这样的。

当我把它读入我的数组时" two_bytes"它总共有16个字符,总价值为
,我会正确读取这些值,但是当我尝试将它们打印出来时,它会打印出类似的东西:

0x4d 0x5a 0xffffff90 0x00 0x03 ....等等。我不知道为什么。怎么可以

我更正


FILE * fp = NULL;

int count = 0;

char nibble = NULL;

char two_bytes [16];

int i;

fp = fopen(" xyz.exe" ,r);

if(fp == NULL)

{

printf(无法打开文件\ n ;);

退出(0);

}

while(!feof(fp))

{

fscanf(fp,"%c",& two_bytes [count ++]);

if(count%16 == 0)

{

for(i = 0; i< count; i ++)

printf("%#x",two_bytes [i]);

printf(" ||");

for(i = 0; i< count; i ++)

printf("%4c" ,two_bytes [i]);

count = 0;

printf(" \ n");

}

}

fclose(fp);

返回0;

Hi,
I am trying to read an exe file and print it out character by
character in hexadecimal format. The file goes something like this in
hexadecimal
0x4d 0x5a 0x90 0x00 0x03 .... so on
When I read it into my array " two_bytes" which has 16 characters in
total, I get each of these values read correctly, but when I attempt
printing them out, it prints something like this:
0x4d 0x5a 0xffffff90 0x00 0x03 .... so on. I have no clue why. How can
I correct this

FILE *fp = NULL;
int count = 0;
char nibble = NULL;
char two_bytes[16];
int i;
fp = fopen("xyz.exe","r");
if(fp == NULL)
{
printf("Unable to Open file\n");
exit(0);
}
while(!feof(fp))
{
fscanf(fp,"%c",&two_bytes[count++]);
if(count % 16 == 0)
{
for(i=0; i<count ; i++)
printf("%#x ",two_bytes[i]);
printf(" || ");
for(i=0; i<count ; i++)
printf("%4c ",two_bytes[i]);
count = 0;
printf("\n");
}
}
fclose(fp);
return 0;

推荐答案

ab ************ @ gmail.com 写道:

char two_bytes [16];

(...)

printf("%#x" ;,two_bytes [I]);
char two_bytes[16];
(...)
printf("%#x ",two_bytes[i]);



''char''可以签名。负char被提升为int,并且
%#x将其打印为unsigned int,因此char 0x90变为0xffffff90。


使用unsigned char two_bytes [16 ];

或printf("%#x",(unsigned char)two_bytes [i]);


-

问候,

Hallvard

''char'' can be signed. A negative char is promoted to int, and
%#x prints it as unsigned int, thus char 0x90 becomes 0xffffff90.

Use unsigned char two_bytes[16];
or printf("%#x ", (unsigned char) two_bytes[i]);

--
Regards,
Hallvard


文章< 11 **************** *****@n59g2000hsh.googlegroups。 com>,

< ab ************ @ gmail.comwrote:
In article <11*********************@n59g2000hsh.googlegroups. com>,
<ab************@gmail.comwrote:

我正在尝试读取一个exe文件并用十六进制格式的
字符打印出来。这个文件就像这样在十六进制中。
0x4d 0x5a 0x90 0x00 0x03 ....所以在
当我把它读入我的数组时 two_bytes"总共有16个字符,我可以正确读取这些值,但是当我尝试将它们打印出来时,会打印出类似这样的内容:
0x4d 0x5a 0xffffff90 0x00 0x03 ...等等。我不知道为什么。怎么能
我更正这个
I am trying to read an exe file and print it out character by
character in hexadecimal format. The file goes something like this in
hexadecimal
0x4d 0x5a 0x90 0x00 0x03 .... so on
When I read it into my array " two_bytes" which has 16 characters in
total, I get each of these values read correctly, but when I attempt
printing them out, it prints something like this:
0x4d 0x5a 0xffffff90 0x00 0x03 .... so on. I have no clue why. How can
I correct this


> FILE * fp = NULL;
int count = 0;
char nibble = NULL;
char two_bytes [16];
>FILE *fp = NULL;
int count = 0;
char nibble = NULL;
char two_bytes[16];



make two_bytes unsigned char

make two_bytes unsigned char


> int i;
fp = fopen(" xyz.exe"," r");
if(fp == NULL)
{
printf(" Unable to打开文件\ n");
退出(0);
}
while(!feof(fp))
{/ fscanf(fp,"%) C",&安培; two_bytes [计数++]);
> int i;
fp = fopen("xyz.exe","r");
if(fp == NULL)
{
printf("Unable to Open file\n");
exit(0);
}
while(!feof(fp))
{
fscanf(fp,"%c",&two_bytes[count++]);



你在那里犯了一个经典错误。 feof()没有设置,直到

你尝试并且没有读取一个字符 - 它不会向前看

以查看下一次读取是否会失败或不。所以你要去

必须尝试阅读这个角色然后确定是否

读取失败。


有使用单一%c格式的fscanf是没有充分理由的:

请参阅fgetc()。

You''ve made a classic error there. feof() is not set until
you try and fail to read a character -- it does NOT "peek ahead"
to see whether the next read would fail or not. So you are going
to have to try to read the character and then determine whether
the read failed.

There is no good reason to fscanf with a single %c format:
see fgetc().


> if(count%16 == 0)
{
for(i = 0; i< count; i ++)
printf("%#x",two_bytes [i]);
printf(" ||");
for(i = 0; i< count; i ++)
printf("%4c",two_bytes [i]);
> if(count % 16 == 0)
{
for(i=0; i<count ; i++)
printf("%#x ",two_bytes[i]);
printf(" || ");
for(i=0; i<count ; i++)
printf("%4c ",two_bytes[i]);



哦,你不想这样做。如果它是换行符或铃声

或类似的东西怎么办?


你应该首先测试它是一个可打印的角色,如果

那么你可以putchar()它;如果它不是,那么打印

a占位符(''。''是典型的。)


或者代替占位符,如果它不是重要的是,

文本等效值始终是完全相同的长度,你可以

打印一个表示 - 例如,字符对
$ b对于退格键,$ b''\''''''''''''''''''''''''''''''''' ^后跟一个字母

是ASCII控制字符的常用表示。


这只是让你在选择表示时

该字符高于ASCII可打印范围,例如,如果

为0xac ...

Oh, you don''t want to do that. What if it''s a newline or a bell
or something like that?

You should first test that it''s a printable character, and if
it is then you can putchar() it; if it isn''t, then print
a placeholder instead (''.'' is typical.)

Or instead of a placeholder, if it isn''t important that the
text equivilent always be exactly the same length, you could
print a representation -- for example, the character pair
''\'' ''t'' for tab, ''\'' ''b'' for backspace. ^ followed by a letter
is a common representation for ASCII control characters.

That just leaves you with a choice of representations when
the character is above the ASCII printable range, such as if
it is 0xac ...


> count = 0;
printf(" \ n");
}
}
fclose(fp);
返回0;
> count = 0;
printf("\n");
}
}
fclose(fp);
return 0;



-

编程是在你忙于制定其他计划时发生的事情。


--
Programming is what happens while you''re busy making other plans.


谢谢你!!!修好了。我意识到我忘了把它变成一个

unsigned char


3月19日下午5:13,Hallvard B Furuseth< hbfurus ... @ usit.uio.no>

写道:
Thank you !!! That fixed it. I realized I''d forgotten to make it a
"unsigned char"

On Mar 19, 5:13 pm, Hallvard B Furuseth <h.b.furus...@usit.uio.no>
wrote:

abhishekkar ... @ gmail.com写道:
abhishekkar...@gmail.com writes:

char two_bytes [16];

(...)

printf("%#x" ,two_bytes [I]);
char two_bytes[16];
(...)
printf("%#x ",two_bytes[i]);



''char''可以签名。负char被提升为int,并且
%#x将其打印为unsigned int,因此char 0x90变为0xffffff90。


使用unsigned char two_bytes [16 ];

或printf("%#x",(unsigned char)two_bytes [i]);


-

问候,

Hallvard


''char'' can be signed. A negative char is promoted to int, and
%#x prints it as unsigned int, thus char 0x90 becomes 0xffffff90.

Use unsigned char two_bytes[16];
or printf("%#x ", (unsigned char) two_bytes[i]);

--
Regards,
Hallvard



这篇关于问题打印十六进制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆