Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > convert form byte[4] to Int32 while retaining the binary value of the byte array

Reply
Thread Tools

convert form byte[4] to Int32 while retaining the binary value of the byte array

 
 
jeff@foundrymusic.com
Guest
Posts: n/a
 
      12-20-2004
how do you convert form byte[4] to Int32 while retaining the binary
value of the byte array

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      12-20-2004
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> how do you convert form byte[4] to Int32 while retaining the binary
> value of the byte array


First of all, you have to be a bit more specific. For example, you need
to say that your 'byte' has only 8 bits and your Int32 is (probably) 32
bits long. Second, what does "binary value" mean? Is your 'byte' type
signed or unsigned? Third, what do you mean by "convert"? An array of
four chars (signed or unsigned) cannot be _converted_ to a single int in
C++ sense, so you probably mean "how to form a 32-bit value out of 4 8-bit
values and retain the bit pattern of each of 4 source values?" That can
be answered but you need to specify where your 'byte[0]' should go, to the
beginning or to the end of the resulting integer.

See what I mean? Perhaps while trying to understand what you need, you
will find a decent way to "convert" what you need into what you desire
using << and | operators...

What are you trying to accomplish, anyway?

V
 
Reply With Quote
 
 
 
 
Howard
Guest
Posts: n/a
 
      12-20-2004

<(E-Mail Removed)> wrote in message
news:(E-Mail Removed) ups.com...
> how do you convert form byte[4] to Int32 while retaining the binary
> value of the byte array
>


As far as I know, neither "Int32" nor "byte" are defined as types in C++. I
can *guess* how they're defined, but it's only an eductated guess. What do
you mean when you say you want to "retain the binary value"? And in what
way do you want to "convert" the value?

If what you have is an array of 4 8-bit unsigned char values, and you want
to interpret them as a 32-bit signed long, then one way might be to use a
pointer, like this:

unsigned char array[4];
//.. fill that array somehow...

long* pInt32 = (long*)(&array[0]);

Now, *pInt32 will be interpreted as a signed long.

BUT!!! This may not work on your machine! For one thing, the byte ordering
of the bytes in the array may not be correct for an integer representation
on your machine. On a PC, the ordering of bytes is opposite what it is on
the Mac, for example. It all depends upon you KNOWING that the values in
that array are in the correct order in the first place.

(On the other hand, if you know that they're in the OPPOSITE order, you can
always copy them to another array in reverse order, and then do the above!)

There may be other issues, possibly, such as the size of a char and a long
on your machine, and the possibility that you could put some bit pattern
into that array which would not be a valid 32-bit long integer.

In general, it's best to avoid doing this if possible. But, you can try it
and see if it works for you. Remember, though, that the solution likely
won't be portable across machines.

-Howard





 
Reply With Quote
 
Jonathan Mcdougall
Guest
Posts: n/a
 
      12-20-2004
Howard wrote:
> <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed) ups.com...
>
>>how do you convert form byte[4] to Int32 while retaining the binary
>>value of the byte array
>>

>
> As far as I know, neither "Int32" nor "byte" are defined as types in C++. I
> can *guess* how they're defined, but it's only an eductated guess. What do
> you mean when you say you want to "retain the binary value"? And in what
> way do you want to "convert" the value?
>
> If what you have is an array of 4 8-bit unsigned char values, and you want
> to interpret them as a 32-bit signed long, then one way might be to use a
> pointer, like this:


<snip..>

Int32 convert(Byte b[4])
{
return (b[0] << 24) | (b[1] << 16) | (b[2] << | (b[3]);
}

Byte b[4] = { 1, 2, 3, 4 };

Int32 i = convert(b);


Let's not get _that_ picky, people.


Jonathan
 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      12-20-2004
Jonathan Mcdougall wrote:
> Howard wrote:
>
>> <(E-Mail Removed)> wrote in message
>> news:(E-Mail Removed) ups.com...
>>
>>> how do you convert form byte[4] to Int32 while retaining the binary
>>> value of the byte array
>>>

>>
>> As far as I know, neither "Int32" nor "byte" are defined as types in
>> C++. I can *guess* how they're defined, but it's only an eductated
>> guess. What do you mean when you say you want to "retain the binary
>> value"? And in what way do you want to "convert" the value?
>>
>> If what you have is an array of 4 8-bit unsigned char values, and you
>> want to interpret them as a 32-bit signed long, then one way might be
>> to use a pointer, like this:

>
>
> <snip..>
>
> Int32 convert(Byte b[4])
> {
> return (b[0] << 24) | (b[1] << 16) | (b[2] << | (b[3]);
> }
>
> Byte b[4] = { 1, 2, 3, 4 };
>
> Int32 i = convert(b);
>
>
> Let's not get _that_ picky, people.


How did you know the OP didn't want

return (b[3] << 24) | (b[2] << 16) | (b[1] << | (b[0]);

???

V
 
Reply With Quote
 
Howard
Guest
Posts: n/a
 
      12-20-2004

"Jonathan Mcdougall" <(E-Mail Removed)> wrote in message
news0Hxd.22440$(E-Mail Removed)...
> Howard wrote:
>> <(E-Mail Removed)> wrote in message
>> news:(E-Mail Removed) ups.com...
>>
>>>how do you convert form byte[4] to Int32 while retaining the binary
>>>value of the byte array
>>>

>>
>> As far as I know, neither "Int32" nor "byte" are defined as types in C++.
>> I can *guess* how they're defined, but it's only an eductated guess.
>> What do you mean when you say you want to "retain the binary value"? And
>> in what way do you want to "convert" the value?
>>
>> If what you have is an array of 4 8-bit unsigned char values, and you
>> want to interpret them as a 32-bit signed long, then one way might be to
>> use a pointer, like this:

>
> <snip..>
>
> Int32 convert(Byte b[4])
> {
> return (b[0] << 24) | (b[1] << 16) | (b[2] << | (b[3]);
> }
>
> Byte b[4] = { 1, 2, 3, 4 };
>
> Int32 i = convert(b);
>
>
> Let's not get _that_ picky, people.
>


How do you know that the byte ordering is correct? Maybe b[0] is the LSB!

(And of course you're just assuming the types match. Logical assumption,
but not explicitly stated.)

-Howard


 
Reply With Quote
 
Alan
Guest
Posts: n/a
 
      12-20-2004

<(E-Mail Removed)> wrote in message news:(E-Mail Removed) ups.com...
> how do you convert form byte[4] to Int32 while retaining the binary
> value of the byte array


Your question has nothing to do with C++.
(hint: experiment with a union of these two types)



 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      12-20-2004
Alan wrote:
> <(E-Mail Removed)> wrote in message news:(E-Mail Removed) ups.com...
>
>>how do you convert form byte[4] to Int32 while retaining the binary
>>value of the byte array

>
>
> Your question has nothing to do with C++.
> (hint: experiment with a union of these two types)


What's a 'union' outside of C++ context? You are contradicting yourself.
If the OP has to look at the union, the question has _everything_ to do
with C++.

Besides, unions are not for conversions between types. They are only to
save memory when storing different types. See more discussions on that
in all known C++ newsgroups.

V
 
Reply With Quote
 
Alan
Guest
Posts: n/a
 
      12-20-2004

"Victor Bazarov" <(E-Mail Removed)> wrote in message news:CfHxd.12489$(E-Mail Removed)09.us.to. verio.net...
> Alan wrote:
> > <(E-Mail Removed)> wrote in message news:(E-Mail Removed) ups.com...
> >
> >>how do you convert form byte[4] to Int32 while retaining the binary
> >>value of the byte array

> >
> >
> > Your question has nothing to do with C++.
> > (hint: experiment with a union of these two types)

>
> What's a 'union' outside of C++ context? You are contradicting yourself.


Hardly, they are also used in other languages (as you know), e.g. C

> If the OP has to look at the union, the question has _everything_ to do
> with C++.


see above

> Besides, unions are not for conversions between types.


...but they can be used for that in C


 
Reply With Quote
 
Howard
Guest
Posts: n/a
 
      12-20-2004

"Alan" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>
> "Victor Bazarov" <(E-Mail Removed)> wrote in message
> news:CfHxd.12489$(E-Mail Removed)09.us.to. verio.net...
>> Alan wrote:
>> > <(E-Mail Removed)> wrote in message
>> > news:(E-Mail Removed) ups.com...
>> >
>> >>how do you convert form byte[4] to Int32 while retaining the binary
>> >>value of the byte array
>> >
>> >
>> > Your question has nothing to do with C++.
>> > (hint: experiment with a union of these two types)

>>
>> What's a 'union' outside of C++ context? You are contradicting yourself.

>
> Hardly, they are also used in other languages (as you know), e.g. C
>
>> If the OP has to look at the union, the question has _everything_ to do
>> with C++.

>
> see above
>
>> Besides, unions are not for conversions between types.

>
> ..but they can be used for that in C
>
>


The OP asked about doing something in C++ (assumed, since that's where he
posted). You answered in two parts. First, you said his question had
"nothing" to do with C++. Victor rightly asks "why?". In what way does a
question about how to accomplish a common task not a question about how to
accomplish that task in C++, given that that's what we discuss here? Then,
you suggest that somehow the ability to use a union construct to accomplish
the task should be a hint to him. We can only assume that you mean it
should be a hint that the original question has nothing to do with C++. Yet
the union construct does exist in C+, so in what way does it hint that the
OP's question is not about C++? You're suggesting one way to do what he
wants to do (albeit not a good way, IMO), with a construct that is available
in C++, yet saying that this somehow demonstrates that the question you're
proposing an answer to is therefore NOT related to C++. Are you as confused
as I am yet?

-Howard



 
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
How to convert a 4 byte character string to its equivalent 4 byte integer value? Polaris431 C Programming 8 12-04-2006 07:00 AM
Deleting row from GridView causes ArgumentOutOfRangeException during System.Web.UI.WebControls.GridView.set_SelectedIndex(Int32 value) loga123 ASP .Net 2 06-28-2006 08:32 PM
Error: System.ArgumentException: Cannot convert to System.Int32. Simon Harris ASP .Net 2 04-27-2005 10:15 PM
Error: System.ArgumentException: Cannot convert to System.Int32. Simon Harris ASP .Net 0 04-27-2005 07:26 PM
Invalid cast from System.Int32 to System.Byte[]. =?Utf-8?B?SGlmbmkgU2hhaHphcmQ=?= ASP .Net 1 02-15-2005 03:05 PM



Advertisments