端序问题,请帮忙 [英] endian problem, please help

查看:64
本文介绍了端序问题,请帮忙的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




它不是真正的端序问题。我想我必须

会遗漏别的东西...


问题可以减少到不同

以下结果两段代码:

(逐字剪切和粘贴)


1.


* width =(无符号char)fgetc(fp)+

256 *(unsigned char)fgetc(fp)+

65536 *(unsigned char)fgetc(fp)+

16777216L *(无符号字符)fgetc(fp);


产生*宽度== 131072应该是512为

fp指向字节序列00 02 00 00而


2.

* width =(unsigned char)fgetc(fp);

* width + = 256 *( unsigned char)fgetc(fp);

* width + = 65536 *(unsigned char)fgetc(fp);

* width + = 16777216L *(unsigned char)fgetc (fp);


导致预期值512.


我缺少什么?它正在驱使我....... :(


感谢任何暗示!



it''s not really an endian problem. I think I must
be missing something else ...

The problem can be reduced to different
results of the following two segments of codes:
(cut and pasted verbatim)

1.

*width = (unsigned char) fgetc(fp) +
256 * (unsigned char) fgetc(fp) +
65536 * (unsigned char) fgetc(fp) +
16777216L * (unsigned char) fgetc(fp);

yields *width == 131072 which should have been 512 for
fp points to the byte sequence of "00 02 00 00" while

2.
*width =(unsigned char) fgetc(fp);
*width += 256 * (unsigned char) fgetc(fp);
*width += 65536 * (unsigned char) fgetc(fp);
*width += 16777216L * (unsigned char) fgetc(fp);

results in the expected value 512.

What am I missing? It''s driving me ....... :(

Thanks for any hint!

推荐答案

12月10日下午4:11,kolmogo ... @ gmail.com< kolmogo ... @ gmail.com>

写道:
On Dec 10, 4:11 pm, "kolmogo...@gmail.com" <kolmogo...@gmail.com>
wrote:




它不是真正的端序问题。我想我必须

缺少其他东西...


问题可以减少到不同

以下两段代码的结果:

(逐字剪切和粘贴)


1.


* width =(unsigned char)fgetc(fp)+

256 *(无符号字符)fgetc(fp)+

65536 *(无符号字符)fgetc(fp)+

16777216L *(无符号字符)fgetc(fp );


产生*宽度== 131072应该是512,因为

fp指向字节序列00 02 00 00而


2.

* width =(unsigned char)fgetc(fp);

* width + = 256 *(unsigned char)fgetc(fp);

* width + = 65536 *( unsigned char)fgetc(fp);

* width + = 16777216L *(unsigned char)fgetc(fp);


产生预期值512。


我缺少什么?它驱使我....... :(


感谢任何暗示!


it''s not really an endian problem. I think I must
be missing something else ...

The problem can be reduced to different
results of the following two segments of codes:
(cut and pasted verbatim)

1.

*width = (unsigned char) fgetc(fp) +
256 * (unsigned char) fgetc(fp) +
65536 * (unsigned char) fgetc(fp) +
16777216L * (unsigned char) fgetc(fp);

yields *width == 131072 which should have been 512 for
fp points to the byte sequence of "00 02 00 00" while

2.
*width =(unsigned char) fgetc(fp);
*width += 256 * (unsigned char) fgetc(fp);
*width += 65536 * (unsigned char) fgetc(fp);
*width += 16777216L * (unsigned char) fgetc(fp);

results in the expected value 512.

What am I missing? It''s driving me ....... :(

Thanks for any hint!



我'我不是100%肯定,但它可能是评估的顺序(首先

结果是2 * 65536)。来自C89草案:

除非另有说明语法{27}或以后另行指定

(对于函数调用运算符(),&&,||,?:和逗号

运算符) ,子表达式的评估顺序和

中发生的副作用的顺序都是未指定的。

-

WYCIWYG - 你得到的是什么

I''m not 100% sure, but it could be the order of evaluation (first
result is 2*65536). From C89 draft:
"Except as indicated by the syntax{27} or otherwise specified later
(for the function-call operator () , && , || , ?: , and comma
operators), the order of evaluation of subexpressions and the order in
which side effects take place are both unspecified."
--
WYCIWYG - what you C is what you get


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

hi,


it''这不是一个真正的endian问题。我想我必须

会遗漏别的东西......


这个问题可以减少到不同的

以下两段代码的结果:

(逐字剪切和粘贴)


1.


* width =(unsigned char)fgetc(fp)+

256 *(无符号字符)fgetc(fp)+

65536 *(无符号字符)fgetc(fp)+

16777216L *(无符号字符)fgetc(fp );
产生*宽度== 131072,对于

fp,它应该是512,指向字节序列00 02 00 00。而


it''s not really an endian problem. I think I must
be missing something else ...

The problem can be reduced to different
results of the following two segments of codes:
(cut and pasted verbatim)

1.

*width = (unsigned char) fgetc(fp) +
256 * (unsigned char) fgetc(fp) +
65536 * (unsigned char) fgetc(fp) +
16777216L * (unsigned char) fgetc(fp);

yields *width == 131072 which should have been 512 for
fp points to the byte sequence of "00 02 00 00" while



无法保证第一行的fgetc()调用首先调用

。它可能在某些系统上,但允许调用以任何顺序发生
,并且在您的系统上,恰好该订单

不是您想要的。您的版本有四个单独的语句

没有这个问题,因为不允许重新排序语句

(除非编译器知道结果无关紧要) )。

There''s no guarantee that the fgetc() call on the first line gets
called first. It might on some systems, but the calls are allowed to
occur in any order, and on your system, it so happens that that order
is not what you want. Your version with four separate statements does
not have this problem since statements are not allowed to be reordered
(except when the compiler knows it doesn''t matter for the result).


文章< 11 ********************** @ l12g2000cwl .googlegroups .com>,
ko********@gmail.com < ; ko ******** @ gmail.comwrote:
In article <11**********************@l12g2000cwl.googlegroups .com>,
ko********@gmail.com <ko********@gmail.comwrote:

> hi,

它不是真的端序问题。我想我必须错过其他的东西......

问题可以归结为以下两段代码的不同结果:
(剪切和逐字粘贴)
>

it''s not really an endian problem. I think I must
be missing something else ...

The problem can be reduced to different
results of the following two segments of codes:
(cut and pasted verbatim)



为什么不使用fread()?

Why not use fread()?


这篇关于端序问题,请帮忙的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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