访问整数的单个字节 [英] Accessing individual bytes of an integer
问题描述
您好!
我想处理单个字节的整数。我知道整数是
32位且永远都是。有时我想使用32位的整个
,有时我想修改
示例的前8位。对我来说,我认为最好能够像这样声明32位
:
unsigned char bits [4];
当我想把它当作一个32位整数时,我可以做一些这样的事情
吗?
unsigned int bits32 = *((unsigned int *)bits);
我不确定语法。我不需要就地工作。使用副本可以获得
罚款。
提前致谢!
-
Daniel
Hello!
I want to work with individual bytes of integers. I know that ints are
32-bit and will always be. Sometimes I want to work with the entire
32-bits, and other times I want to modify just the first 8-bits for
example. For me, I think it would be best if I can declare the 32-bits
like this:
unsigned char bits[4];
When I want to treat this as a 32-bits integer, can I do something
like this?
unsigned int bits32 = *((unsigned int*)bits);
I''m unsure of the syntax. I don''t need to work in-place so to speak. It is
fine to work with a copy.
Thanks in advance!
--
Daniel
推荐答案
" Daniel Lidstr?m" <所以***** @ microsoft.com>在消息中写道
news:pa **************************** @ microsoft.com。 ..
"Daniel Lidstr?m" <so*****@microsoft.com> wrote in message
news:pa****************************@microsoft.com. ..
当我想把它当作一个32位整数时,我可以这样做吗?
unsigned int bits32 = *((unsigned int *)bits);
When I want to treat this as a 32-bits integer, can I do something
like this?
unsigned int bits32 = *((unsigned int*)bits);
是的,你可以,但你绝对不能保证结果是什么结果
将是:-)
(位>> n)& 0xff
其中n是0,8,16或24?
Yes you can, but you have absolutely no assurance as to what the results
will be :-)
What''s wrong with
(bits>>n) & 0xff
where n is 0, 8, 16, or 24?
" Daniel Lidstr?m <所以***** @ microsoft.com> ????
新闻:pa **************************** @ microsoft.com。 ..
"Daniel Lidstr?m" <so*****@microsoft.com> ????
news:pa****************************@microsoft.com. ..
你好!
我想处理单个字节的整数。我知道这些内容是32位的,并且永远都是。有时我想使用整个
32位,有时我想修改
示例的前8位。对我来说,我认为最好能宣布这样的32位
unsigned char bits [4];
当我想要的时候把它当作一个32位整数,我可以这样做吗?
unsigned int bits32 = *((unsigned int *)bits);
我不确定语法。我不需要就地工作。使用副本是好的。
提前致谢!
-
Daniel
Hello!
I want to work with individual bytes of integers. I know that ints are
32-bit and will always be. Sometimes I want to work with the entire
32-bits, and other times I want to modify just the first 8-bits for
example. For me, I think it would be best if I can declare the 32-bits
like this:
unsigned char bits[4];
When I want to treat this as a 32-bits integer, can I do something
like this?
unsigned int bits32 = *((unsigned int*)bits);
I''m unsure of the syntax. I don''t need to work in-place so to speak. It is
fine to work with a copy.
Thanks in advance!
--
Daniel
无需考虑字节顺序,你是对的。
更好的方法是使用像这样的联盟:
union xxx
{
unsigned char bits [4];
unsigned int i;
};
Unconsidering the byte sequence, you are correct.
A better way is using a union like:
union xxx
{
unsigned char bits[4];
unsigned int i;
};
MatrixV写道:
MatrixV wrote:
" Daniel Lidstr?m" <所以***** @ microsoft.com> ????
新闻:pa **************************** @ microsoft.com。 ..
"Daniel Lidstr?m" <so*****@microsoft.com> ????
news:pa****************************@microsoft.com. ..
你好!
我想处理单个字节的整数。我知道这些内容是32位的,并且永远都是。有时我想使用整个
32位,有时我想修改
示例的前8位。对我来说,我认为最好能宣布这样的32位
unsigned char bits [4];
当我想要的时候把它当作一个32位整数,我可以这样做吗?
unsigned int bits32 = *((unsigned int *)bits);
我不确定语法。我不需要就地工作。使用副本是好的。
提前致谢!
-
Daniel
Hello!
I want to work with individual bytes of integers. I know that ints are
32-bit and will always be. Sometimes I want to work with the entire
32-bits, and other times I want to modify just the first 8-bits for
example. For me, I think it would be best if I can declare the 32-bits
like this:
unsigned char bits[4];
When I want to treat this as a 32-bits integer, can I do something
like this?
unsigned int bits32 = *((unsigned int*)bits);
I''m unsure of the syntax. I don''t need to work in-place so to speak. It is
fine to work with a copy.
Thanks in advance!
--
Daniel
无需考虑字节序列,你是对的。
更好的方法是使用如下的联合:
union xxx
{
unsigned char bits [4];
unsigned int i;
};
Unconsidering the byte sequence, you are correct.
A better way is using a union like:
union xxx
{
unsigned char bits[4];
unsigned int i;
};
怎么样:
union xxx
{
unsigned char bytes [sizeof(unsigned int))];
unsigned int i;
};
这不假设整数中有多少字节是
。
-
Thomas Matthews
C ++新闻组欢迎信息:
http: //www.slack.net/~shiva/welcome.txt
C ++常见问题: http://www.parashift.com/c++-faq-lite
C常见问题: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c ++ faq:
http://www.comeaucomputing.com/learn/faq/
其他网站:
http://www.josuttis.com - C ++ STL图书馆书籍
http://www.sgi.com/tech/stl - 标准模板库
How about this:
union xxx
{
unsigned char bytes[sizeof(unsigned int))];
unsigned int i;
};
This makes no assumptions about how many bytes are
in an integer.
--
Thomas Matthews
C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.comeaucomputing.com/learn/faq/
Other sites:
http://www.josuttis.com -- C++ STL Library book
http://www.sgi.com/tech/stl -- Standard Template Library
这篇关于访问整数的单个字节的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!