可移植地访问1个字节 [英] Portably accessing 1 byte

查看:86
本文介绍了可移植地访问1个字节的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我正在开发一个程序,当然我希望它可以像

一样便携。我需要能够访问其中的特定字节数,

但据我所知,c语言中没有关键字,例如

''字节''。是否总是可以假设char数据类型总是

1字节,或者是否有其他方法可以确保你获得1字节

不是处理器/ OS依赖哪个更好,或者没有办法

绝对可靠和绝对便携?


感谢您的帮助。

Hi

I''m developing a program, and naturally I want it to be as portable as
possible. I need to be able to access specific numbers of bytes in it,
but as far as I know, there is no keyword in the c language such as
''byte''. Is it always okay to assume that the char data type is always
1 byte, or is there some other way to be sure you are getting 1 byte
that is not processor/OS dependent that is better, or is there no way
to be both absolutly sure and absolutly portable?

Thanks for any help.

推荐答案

rsood写道:
我正在开发一个程序,我自然希望它像便携式一样
可能。我需要能够访问其中的特定字节数,
但据我所知,c语言中没有关键字,例如
''byte''。是否总是可以假设char数据类型始终是1个字节,或者是否有其他方法可以确保获得1个字节
不依赖于处理器/ OS的更好,或者有没有办法绝对可靠和绝对便携?
I''m developing a program, and naturally I want it to be as portable as
possible. I need to be able to access specific numbers of bytes in it,
but as far as I know, there is no keyword in the c language such as
''byte''. Is it always okay to assume that the char data type is always
1 byte, or is there some other way to be sure you are getting 1 byte
that is not processor/OS dependent that is better, or is there no way
to be both absolutly sure and absolutly portable?




sizeof(char)总是1,不是吗? sizeof返回字节,所以我认为

这是你的基本字节类型。



sizeof(char) is always 1, isn''t it? sizeof returns bytes, so I think
this is your basic byte type.





rsood在05/05/06 14:57写道:


rsood wrote On 05/05/06 14:57,:


我正在开发一个程序,我自然希望它像
可能。我需要能够访问其中的特定字节数,
但据我所知,c语言中没有关键字,例如
''byte''。是否总是可以假设char数据类型始终是1个字节,或者是否有其他方法可以确保获得1个字节
不依赖于处理器/ OS的更好,或者有没有办法绝对可靠和绝对便携?
Hi

I''m developing a program, and naturally I want it to be as portable as
possible. I need to be able to access specific numbers of bytes in it,
but as far as I know, there is no keyword in the c language such as
''byte''. Is it always okay to assume that the char data type is always
1 byte, or is there some other way to be sure you are getting 1 byte
that is not processor/OS dependent that is better, or is there no way
to be both absolutly sure and absolutly portable?




一个字符是一个字节,永远和永远。

但是一个字节可能没有你期望的意思。

在C中,一个字节至少有八位,但可能还有多少
。 9位字节的机器现在很少见,但是一旦相当常见,它们就是

,即使在今天,也可以找到32位字节的机器。


如果你想要的是一个八位字节,也就是说,一个8位
的数量,你可以将值存储在一个byte / char中,但是你需要在操作它时稍微小心。例如,如果你反复开始递增一个'unsigned char',那么



在原来的

值之前它可能需要超过256个增量重新出现。


-
Er ****** ***@sun.com



One char is one byte, always and forever.

BUT "one byte" might not have the meaning you expect.
In C, "one byte" has at least eight bits, but might have
more. Machines with 9-bit bytes are now rare but were
once fairly common, and machines with 32-bit bytes can be
found even today.

If what you want is "one octet," that is, one eight-bit
quantity, you can store the value in a byte/char but you
must use a little care in manipulating it. For example,
if you start incrementing an `unsigned char'' repeatedly,
it might take more than 256 increments before the original
value reappears.

--
Er*********@sun.com


-----开始PGP签名消息-----

哈希:SHA1


void * clvrmnky()写道:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

void * clvrmnky() wrote:
rsood写道:
我正在开发一个程序,我自然希望它像便携式一样便携。我需要能够访问其中的特定字节数,
但据我所知,c语言中没有关键字,例如
''byte''。是否总是可以假设char数据类型始终是1个字节,或者是否有其他方法可以确保获得1个字节
不依赖于处理器/ OS的更好,或者有没有办法绝对可靠和绝对便携?
I''m developing a program, and naturally I want it to be as portable as
possible. I need to be able to access specific numbers of bytes in it,
but as far as I know, there is no keyword in the c language such as
''byte''. Is it always okay to assume that the char data type is always
1 byte, or is there some other way to be sure you are getting 1 byte
that is not processor/OS dependent that is better, or is there no way
to be both absolutly sure and absolutly portable?



sizeof(char)总是1,不是吗? sizeof返回字节,所以我认为
这是你的基本字节类型。



sizeof(char) is always 1, isn''t it? sizeof returns bytes, so I think
this is your basic byte type.




足够真实。


是OP对C''bytes''感兴趣(它与''char''同义,

是/至少/ 8位宽),或者他对8位数量感兴趣

(有时在C竞技场之外称为''字节')?

如果他对C''字节'感兴趣,那么char将会do

如果他只对8位数量感兴趣,那么(AFAIK)就没有

/ portable / way来管理它。
< br $> b $ b - -

Lew Pitcher,IT专家,企业技术解决方案,

企业技术解决方案,道明银行金融集团/>

(此处表达的意见是我自己的,而不是我的雇主的意见)

-----开始PGP签名-----

版本:GnuPG v1.4.2.2(MingW32)

评论:将GnuPG与Mozilla一起使用 - http://enigmail.mozdev.org

iD8DBQFEW6oBagVFX4UWr64RAnExAJ9MbVIA6oI3jI3d4CUHwv KTU8zm8QCdH + 07

TDaIF2lv4fvBUlpBVyjT4Ww =

= JA / 7

----- END PGP SIGNATURE -----



True enough.

Is the OP interested in C ''bytes'' (which are synonymous with ''char'', and
are /at least/ 8 bits wide), or is he interested in 8-bit quantities
(which are sometimes called ''bytes'' outside of the C arena)?

If he is interested in C ''bytes'' then char will do
If he is interested in 8-bit quantities only, then (AFAIK) there is no
/portable/ way to manage that in C.

- --

Lew Pitcher, IT Specialist, Corporate Technology Solutions,
Enterprise Technology Solutions, TD Bank Financial Group

(Opinions expressed here are my own, not my employer''s)
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2.2 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFEW6oBagVFX4UWr64RAnExAJ9MbVIA6oI3jI3d4CUHwv KTU8zm8QCdH+07
TDaIF2lv4fvBUlpBVyjT4Ww=
=JA/7
-----END PGP SIGNATURE-----


这篇关于可移植地访问1个字节的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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