 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
jeff@foundrymusic.com Guest
|
Posted: Tue Dec 21, 2004 10:20 am Post subject: howto convert form byte[4] to Int32 while retaining the bina |
|
|
How do you convert form byte[4] to Int32 while retaining the binary
value of the byte array?
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Matthew Hanna Guest
|
Posted: Wed Dec 22, 2004 8:56 am Post subject: Re: howto convert form byte[4] to Int32 while retaining the |
|
|
wouldn't memcpy work?
[email]jeff (AT) foundrymusic (DOT) com[/email] wrote:
| Quote: | How do you convert form byte[4] to Int32 while retaining the binary
value of the byte array?
|
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Ben Hutchings Guest
|
Posted: Wed Dec 22, 2004 9:01 am Post subject: Re: howto convert form byte[4] to Int32 while retaining the |
|
|
[email]jeff (AT) foundrymusic (DOT) com[/email] wrote:
| Quote: | How do you convert form byte[4] to Int32
|
Presumably byte is a type alias for char (or unsigned char?) which you
assume to have 8 bits and Int32 is a type alias for a 32-bit integer?
There are several ways of converting between multiple bytes and larger
integers; which do you want?
| Quote: | while retaining the binary value of the byte array?
|
What do you mean by that?
--
Ben Hutchings
I'm not a reverse psychological virus. Please don't copy me into your sig.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
John Dumais Guest
|
Posted: Wed Dec 22, 2004 9:03 am Post subject: Re: howto convert form byte[4] to Int32 while retaining the |
|
|
You could try something like...
{
union {
unsigned char c[4];
long l;
} u ;
u.c[0] = 1;
u.c[1] = 2;
u.c[2] = 3;
u.c[3] = 4;
// use the integer value
std::cout << u.l << std::endl;
}
[email]jeff (AT) foundrymusic (DOT) com[/email] wrote:
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Ron Natalie Guest
|
Posted: Wed Dec 22, 2004 9:04 am Post subject: Re: howto convert form byte[4] to Int32 while retaining the |
|
|
[email]jeff (AT) foundrymusic (DOT) com[/email] wrote:
| Quote: | How do you convert form byte[4] to Int32 while retaining the binary
value of the byte array?
|
Depends on the system. There's no portable way to do this (depending
on what you are really asking for).
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Antoun Kanawati Guest
|
Posted: Wed Dec 22, 2004 9:05 am Post subject: Re: howto convert form byte[4] to Int32 while retaining the |
|
|
[email]jeff (AT) foundrymusic (DOT) com[/email] wrote:
| Quote: | How do you convert form byte[4] to Int32 while retaining the binary
value of the byte array?
|
First, why should anything happen to the byte array?
Second, what order are these bytes in?
--
A. Kanawati
[email]NO.antounk.SPAM (AT) comcast (DOT) net[/email]
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Michael Jørgensen Guest
|
Posted: Wed Dec 22, 2004 9:05 am Post subject: Re: howto convert form byte[4] to Int32 while retaining the |
|
|
<jeff (AT) foundrymusic (DOT) com> wrote
| Quote: | How do you convert form byte[4] to Int32 while retaining the binary
value of the byte array?
|
What do you mean by "binary value of the byte array"?
Well, this is a matter of interpretation, better known as "endianness".
You have four bytes, byte[0], byte[1], byte[2], and byte[3].
If the bytes are ordered in little-endian, then the result is
int32_t value = (byte[3] << 24) + (byte[2] << 16) + (byte[1] << +
byte[0]; // little-endian.
If the bytes are ordered in big-endian, then the result is:
int32_t value = (byte[0] << 24) + (byte[1] << 16) + (byte[2] << +
byte[3]; // big-endian.
Note, this has nothing to do with the endianness of the CPU; it depends
solely on the semantics *you* impose on the byte array.
The above is the cheap, simple, and portable. However, it does not perform
very well. There are techniques that are substantially quicker, at the
expense of losing portability.
Many networking protocols - for instance - use big endian byte arrays. One
approach is to reinterpret the byte array into the CPUs endianness, and then
convert to big-endian:
int32_t value = ntohl( *(int32_t *) &byte[0] ); // big-endian.
-Michael.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
John Dumais Guest
|
|
| Back to top |
|
 |
Rade Guest
|
Posted: Thu Dec 23, 2004 9:16 pm Post subject: Re: howto convert form byte[4] to Int32 while retaining the |
|
|
<jeff (AT) foundrymusic (DOT) com> wrote
| Quote: | How do you convert form byte[4] to Int32 while retaining the binary
value of the byte array?
|
But what if OP has mixed C++ and C# - there are two rather basic C# types
called byte and Int32 ?
If this is the case, the whole discussion is off-topic (both here and in
c.l.c++) <OT>, and I'd recommend the OP to look at the System.BitConverter
class in the MSDN</OT>.
Rade
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
kanze@gabi-soft.fr Guest
|
Posted: Thu Dec 23, 2004 9:47 pm Post subject: Re: howto convert form byte[4] to Int32 while retaining the |
|
|
Michael Jørgensen wrote:
| Quote: | jeff (AT) foundrymusic (DOT) com> wrote in message
news:1103573700.383982.27200 (AT) z14g2000cwz (DOT) googlegroups.com...
How do you convert form byte[4] to Int32 while retaining the binary
value of the byte array?
What do you mean by "binary value of the byte array"?
Well, this is a matter of interpretation, better known as
"endianness".
|
Endianness is really only the tip of the iceberg. First, of course,
with regards to four byte ints (which are far from universal -- I've
seen 2 and 4 personally, and have heard of machines using 1 and 6, and
maybe one with 3), there are 24 different orders possible. Some are
pretty improbable, but I've seen at least 3. Then, there is a question
of representation : most modern machines are two's complement, but at
least one is one's complement, and a signed magnitude machine was sold
not so long ago. Not to mention that depending on where the bytes came
from, they may not correspond to any hardware format what so ever.
| Quote: | You have four bytes, byte[0], byte[1], byte[2], and byte[3].
If the bytes are ordered in little-endian, then the result is
int32_t value = (byte[3] << 24) + (byte[2] << 16) + (byte[1] << +
byte[0]; // little-endian.
If the bytes are ordered in big-endian, then the result is:
int32_t value = (byte[0] << 24) + (byte[1] << 16) + (byte[2] << +
byte[3]; // big-endian.
Note, this has nothing to do with the endianness of the CPU; it
depends solely on the semantics *you* impose on the byte array.
|
Or more often, that someone else outside the project has decided to
impose, without asking you:-).
| Quote: | The above is the cheap, simple, and portable. However, it does not
perform very well. There are techniques that are substantially
quicker, at the expense of losing portability.
|
Are you sure? I've never had any performance problems with it.
--
James Kanze GABI Software http://www.gabi-soft.fr
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Daniel M. Pfeffer Guest
|
Posted: Mon Jul 18, 2005 8:25 am Post subject: Re: howto convert form byte[4] to Int32 while retaining the |
|
|
<jeff (AT) foundrymusic (DOT) com> wrote
| Quote: | How do you convert form byte[4] to Int32 while retaining the binary
value of the byte array?
|
You can't do this in any portable way. However, if I make the assumptions
1. byte is a synonym for unsigned char (e.g. typedef unsigned char byte;)
2. Int32 is a synonym for an (n*CHAR_BITS)-bit signed integer (see
<climits> for the value of CHAR_BITS)
3. There are no unused bits or trap values in the representation of Int32
then the following code should work for reading and writing values in a
particular runtime environment (=CPU + operating system + compiler +
compiler settings):
union x {
byte b[sizeof(Int32)];
Int32 i;
};
writing a value into the i member and reading it out of the b member (or
vice versa) should provide the desired conversion.
HTH
Daniel Pfeffer
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|