Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Accessing individual bytes of an integer

Reply
Thread Tools

Accessing individual bytes of an integer

 
 
=?iso-8859-1?q?Daniel_Lidstr=F6m?=
Guest
Posts: n/a
 
      02-17-2005
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

 
Reply With Quote
 
 
 
 
Andrew Koenig
Guest
Posts: n/a
 
      02-17-2005
"Daniel Lidström" <(E-Mail Removed)> wrote in message
news(E-Mail Removed). ..

> When I want to treat this as a 32-bits integer, can I do something
> like this?
>
> unsigned int bits32 = *((unsigned int*)bits);


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?


 
Reply With Quote
 
 
 
 
MatrixV
Guest
Posts: n/a
 
      02-17-2005

"Daniel Lidström" <(E-Mail Removed)> ????
news(E-Mail Removed). ..
> 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
>


Unconsidering the byte sequence, you are correct.
A better way is using a union like:
union xxx
{
unsigned char bits[4];
unsigned int i;
};


 
Reply With Quote
 
Thomas Matthews
Guest
Posts: n/a
 
      02-17-2005
MatrixV wrote:
> "Daniel Lidström" <(E-Mail Removed)> ????
> news(E-Mail Removed). ..
>
>>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
>>

>
>
> Unconsidering the byte sequence, you are correct.
> A better way is using a union like:
> union xxx
> {
> unsigned char bits[4];
> unsigned int i;
> };
>
>


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

 
Reply With Quote
 
Andrew Koenig
Guest
Posts: n/a
 
      02-17-2005
"MatrixV" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...

> Unconsidering the byte sequence, you are correct.
> A better way is using a union like:
> union xxx
> {
> unsigned char bits[4];
> unsigned int i;
> };


Not really. When you use a union, you have no assurance about the effect
that giving a value to one member of a union will have on other members.


 
Reply With Quote
 
Old Wolf
Guest
Posts: n/a
 
      02-17-2005
MatrixV wrote:
> "Daniel Lidström" <(E-Mail Removed)> ????
> >
> > 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);


Bad - if 'bits' is not correctly aligned for an int, then
you have undefined behaviour.

> > 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.


You can work in-place with:
unsigned int bits32;
and then to access the chars:
((unsigned char *)&bits32)[0]
etc. Note that the contents of the chars could be anything
(eg. big endian, little endian, or something more exotic),
and if you modify one of those chars then you aren't guaranteed
to have anything sensible left in bits32.

If you don't want to work in-place then you could memcpy
between the int and the char (with the same caveats I mentioned
already).

To work portably (assuming a 32-bit int and 8-bit char),
you can use bit-shifts and masks to extract the four bytes
and replace them. A good compiler would optimise this code
into a single instruction, if it could.

> Unconsidering the byte sequence, you are correct.
> A better way is using a union like:
> union xxx
> {
> unsigned char bits[4];
> unsigned int i;
> };


Undefined behaviour if you access a member of a union that
wasn't the one you just set.

 
Reply With Quote
 
Clark S. Cox III
Guest
Posts: n/a
 
      02-17-2005
On 2005-02-17 15:38:50 -0500, "Andrew Koenig" <(E-Mail Removed)> said:

> "MatrixV" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>
>> Unconsidering the byte sequence, you are correct.
>> A better way is using a union like:
>> union xxx
>> {
>> unsigned char bits[4];
>> unsigned int i;
>> };

>
> Not really. When you use a union, you have no assurance about the
> effect that giving a value to one member of a union will have on other
> members.


You do when one of them is an array of unsigned char.

--
Clark S. Cox, III
http://www.velocityreviews.com/forums/(E-Mail Removed)

 
Reply With Quote
 
Ioannis Vranos
Guest
Posts: n/a
 
      02-17-2005
Daniel Lidström wrote:

> 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);



Yes but not like this because array bits is not initialised.



> 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.



What you can do is read an unsigned int or any other POD type as a
sequence of unsigned chars (or plain chars) - that is bytes, copy it
byte by byte to another unsigned char sequence (which includes possible
padding bits), and deal the new char sequence as another unsigned int.


The following example uses an int and is portable:


#include <iostream>

int main()
{
int integer=0;

unsigned char *puc= reinterpret_cast<unsigned char *>(&integer);


unsigned char otherInt[sizeof(integer)];

// Read integer byte by byte and copy it to otherInt
for(unsigned i=0; i<sizeof(integer); ++i)
otherInt[i]= puc[i];


// We treat the new unsigned char sequence as an int
int *p= reinterpret_cast<int *>(otherInt);

// Assign another value to the integer otherInt!
*p=7;

std::cout<<*p<<"\n";
}




--
Ioannis Vranos

http://www23.brinkster.com/noicys
 
Reply With Quote
 
Jack Klein
Guest
Posts: n/a
 
      02-18-2005
On Thu, 17 Feb 2005 19:16:24 +0100, Daniel Lidström
<(E-Mail Removed)> wrote in comp.lang.c++:

> Hello!
>
> I want to work with individual bytes of integers. I know that ints are
> 32-bit and will always be.


No, you don't. You just think you do. But you are mistaken.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
 
Reply With Quote
 
Andrew Koenig
Guest
Posts: n/a
 
      02-18-2005
>> Not really. When you use a union, you have no assurance about the effect
>> that giving a value to one member of a union will have on other members.


> You do when one of them is an array of unsigned char.


Can you show me where in the C++ standard it says that? The text that I
think is relevant can be found in subclause 9.5:

In a union, at most one of the data members can be active at any time, that
is, the value of at most one of the data members can be stored in a union at
any time. [Note: one special guarantee is made in order to simplify the use
of unions: If a POD-union contains several POD-structs that share a common
initial sequence (9.2), and if an object of this POD-union type contains one
of the POD-structs, it is permitted to inspect the common initial sequence
of any of POD-struct members; see 9.2. ]

I think that "Only one of the data members can be active at any time" is
pretty clear, and the one exception to that rule says nothing about array of
unsigned character.





 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Access individual bytes of a 4 byte long (optimization) anon.asdf@gmail.com C Programming 17 08-12-2007 06:09 AM
How to access the individual bytes of LONGLONG value shaji C Programming 10 03-22-2006 05:48 AM
Ratio of Bytes Delayed to Bytes Sent netproj Cisco 0 12-21-2005 08:08 PM
Converting a String object to individual bytes Erinys Javascript 1 11-16-2004 05:38 PM
Private Bytes vs. # Bytes in all Heaps in Perfmon Jason Collins ASP .Net 3 02-18-2004 03:59 PM



Advertisments