C如何处理Endianness引起的问题? [英] How does C handle issues arising out of Endianness?

查看:52
本文介绍了C如何处理Endianness引起的问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



如果我是对的,则Endianness与CPU有关。我不知道

问题本身是否正确,但如果是,那么C如何处理因Endianness而产生的问题




我明白如果我们跨平台使用套接字传递结构,

我们需要在应用程序级别处理Endianness问题。但是

例如,对于使用按位AND的代码来判断一个数字是否是奇数或偶数,C如何知道LSB的位置?


谢谢,

IC

Hi,
If I am right Endianness is CPU related. I do not know if the
question is right in itself but if it is then how does C handle issues
arising out of Endianness.

I understand that if we pass structures using sockets across platforms,
we need to take care of Endianness issues at the application level. But
for example, for the code using bitwise AND to figure out if a number
is odd or even, how does C know the LSB position?

Thanks,
IC

推荐答案

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



如果我是对的,则Endianness与CPU有关。我不知道

问题本身是否正确,但如果是,那么C如何处理因Endianness而产生的问题




我明白如果我们跨平台使用套接字传递结构,

我们需要在应用程序级别处理Endianness问题。但是

例如,对于使用按位AND的代码来判断一个数字是否是奇数或偶数,C如何知道LSB的位置?
Hi,
If I am right Endianness is CPU related. I do not know if the
question is right in itself but if it is then how does C handle issues
arising out of Endianness.

I understand that if we pass structures using sockets across platforms,
we need to take care of Endianness issues at the application level. But
for example, for the code using bitwise AND to figure out if a number
is odd or even, how does C know the LSB position?



C依靠实现者为每个平台定义每个本机数据的每个运算符

类型。例如,您可以查找

gcc / config / * / *。md(机器描述)文件。

标准C有针对数据类型惩罚的规则你的奇数/偶数

代码会因字节顺序的变化而中断。 C不一定

阻止你违反这些规则。

C relies on the implementor to define each operator for each native data
type for each platform. For an example, you could look up the
gcc/config/*/*.md (machine description) files.
Standard C has rules against data type punning under which your odd/even
code would break with a change of endianness. C can''t necessarily
prevent you from breaking those rules.


谢谢。我将检查它。
Thanks. I will check it out.

C依赖于实现者为每个平台定义每个本机数据的每个运算符

类型。
C relies on the implementor to define each operator for each native data
type for each platform.



那么为什么结构不同呢?为什么程序员

会在跨平台传递它的同时处理它?它更像是一个b $ b的理由吗?相关问题?


谢谢,

IC

So why does it not do the same with structs? Why should the programmer
take care of it while passing it across platforms? Is it more of a
"rationale" related question?

Thanks,
IC


在************ @ gmail.com 中写道:
In************@gmail.com wrote:



如果我是对的,则Endianness与CPU有关。我不知道

问题本身是否正确,但如果是,那么C如何处理由Endianness引起的问题


Hi,
If I am right Endianness is CPU related. I do not know if the
question is right in itself but if it is then how does C handle issues
arising out of Endianness.



忽略它们。

By ignoring them.


我明白如果我们跨平台使用套接字传递结构,

我们需要在应用程序级别处理Endianness问题。但是

例如,对于使用按位AND的代码来判断数字是否为奇数或偶数,C如何知道LSB位置?
I understand that if we pass structures using sockets across platforms,
we need to take care of Endianness issues at the application level. But
for example, for the code using bitwise AND to figure out if a number
is odd or even, how does C know the LSB position?



在任何特定实现中,被测试的未知

值的LSB与

常数1你正在与它进行联系。问题解决了。


当您在不同的

实现之间交换数据时可能会出现问题,因为他们可能不同意字节顺序。他们

也可能不同意其他代表事项:一个

平台可能代表一个16位的int而另一个

使用三十个二,一个可能使用IEEE浮点而另一个使用S / 360格式,两个可能在结构中插入填充

不同,依此类推。 Endianness只是在不同系统之间进行通信时必须考虑的一些

代表性问题之一。


一种方法已经证明广泛有用的是发明一种

有线格式。对于要交换的数据,

的格式不取决于机器的特性。每台机器

则需要两个例程:一个用于读取有线格式。并转换

将其转换为本机表示,并将其转换为原始格式

为有线格式。出于显而易见的原因,许多极端流行的

有线格式使用文本表示:如果你想发送价值四十二的

,你传送两个字符4和2,

可能跟随通过像''\ n''或'';''或者其他类似的分隔符。

这并不能解决所有可能的问题(因为编码

角色也可能因机器而异,但它解决了很多其中很多,并且通常会留下一个相当易处理的残余

来处理。
< br $>
-

Eric Sosman
es *** **@acm-dot-org.inva 盖子


这篇关于C如何处理Endianness引起的问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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