convert form byte[4] to Int32 while retaining the binary value of the byte array | | |
how do you convert form byte[4] to Int32 while retaining the binary
value of the byte array | | | | re: convert form byte[4] to Int32 while retaining the binary value of the byte array jeff@foundrymusic.com wrote:[color=blue]
> how do you convert form byte[4] to Int32 while retaining the binary
> value of the byte array[/color]
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 | | | | re: convert form byte[4] to Int32 while retaining the binary value of the byte array
<jeff@foundrymusic.com> wrote in message
news:1103573749.208215.95260@f14g2000cwb.googlegro ups.com...[color=blue]
> how do you convert form byte[4] to Int32 while retaining the binary
> value of the byte array
>[/color]
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 | | | | re: convert form byte[4] to Int32 while retaining the binary value of the byte array
Howard wrote:[color=blue]
> <jeff@foundrymusic.com> wrote in message
> news:1103573749.208215.95260@f14g2000cwb.googlegro ups.com...
>[color=green]
>>how do you convert form byte[4] to Int32 while retaining the binary
>>value of the byte array
>>[/color]
>
> 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:[/color]
<snip..>
Int32 convert(Byte b[4])
{
return (b[0] << 24) | (b[1] << 16) | (b[2] << 8) | (b[3]);
}
Byte b[4] = { 1, 2, 3, 4 };
Int32 i = convert(b);
Let's not get _that_ picky, people.
Jonathan | | | | re: convert form byte[4] to Int32 while retaining the binary value of the byte array
Jonathan Mcdougall wrote:[color=blue]
> Howard wrote:
>[color=green]
>> <jeff@foundrymusic.com> wrote in message
>> news:1103573749.208215.95260@f14g2000cwb.googlegro ups.com...
>>[color=darkred]
>>> how do you convert form byte[4] to Int32 while retaining the binary
>>> value of the byte array
>>>[/color]
>>
>> 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:[/color]
>
>
> <snip..>
>
> Int32 convert(Byte b[4])
> {
> return (b[0] << 24) | (b[1] << 16) | (b[2] << 8) | (b[3]);
> }
>
> Byte b[4] = { 1, 2, 3, 4 };
>
> Int32 i = convert(b);
>
>
> Let's not get _that_ picky, people.[/color]
How did you know the OP didn't want
return (b[3] << 24) | (b[2] << 16) | (b[1] << 8) | (b[0]);
???
V | | | | re: convert form byte[4] to Int32 while retaining the binary value of the byte array
<jeff@foundrymusic.com> wrote in message news:1103573749.208215.95260@f14g2000cwb.googlegro ups.com...[color=blue]
> how do you convert form byte[4] to Int32 while retaining the binary
> value of the byte array[/color]
Your question has nothing to do with C++.
(hint: experiment with a union of these two types) | | | | re: convert form byte[4] to Int32 while retaining the binary value of the byte array
"Jonathan Mcdougall" <jonathanmcdougall@DELyahoo.ca> wrote in message
news:o0Hxd.22440$Yb.1199029@weber.videotron.net...[color=blue]
> Howard wrote:[color=green]
>> <jeff@foundrymusic.com> wrote in message
>> news:1103573749.208215.95260@f14g2000cwb.googlegro ups.com...
>>[color=darkred]
>>>how do you convert form byte[4] to Int32 while retaining the binary
>>>value of the byte array
>>>[/color]
>>
>> 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:[/color]
>
> <snip..>
>
> Int32 convert(Byte b[4])
> {
> return (b[0] << 24) | (b[1] << 16) | (b[2] << 8) | (b[3]);
> }
>
> Byte b[4] = { 1, 2, 3, 4 };
>
> Int32 i = convert(b);
>
>
> Let's not get _that_ picky, people.
>[/color]
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 | | | | re: convert form byte[4] to Int32 while retaining the binary value of the byte array
Alan wrote:[color=blue]
> <jeff@foundrymusic.com> wrote in message news:1103573749.208215.95260@f14g2000cwb.googlegro ups.com...
>[color=green]
>>how do you convert form byte[4] to Int32 while retaining the binary
>>value of the byte array[/color]
>
>
> Your question has nothing to do with C++.
> (hint: experiment with a union of these two types)[/color]
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 | | | | re: convert form byte[4] to Int32 while retaining the binary value of the byte array
"Victor Bazarov" <v.Abazarov@comAcast.net> wrote in message news:CfHxd.12489$Ae.5353@newsread1.dllstx09.us.to. verio.net...[color=blue]
> Alan wrote:[color=green]
> > <jeff@foundrymusic.com> wrote in message news:1103573749.208215.95260@f14g2000cwb.googlegro ups.com...
> >[color=darkred]
> >>how do you convert form byte[4] to Int32 while retaining the binary
> >>value of the byte array[/color]
> >
> >
> > Your question has nothing to do with C++.
> > (hint: experiment with a union of these two types)[/color]
>
> What's a 'union' outside of C++ context? You are contradicting yourself.[/color]
Hardly, they are also used in other languages (as you know), e.g. C
[color=blue]
> If the OP has to look at the union, the question has _everything_ to do
> with C++.[/color]
see above
[color=blue]
> Besides, unions are not for conversions between types.[/color]
...but they can be used for that in C | | | | re: convert form byte[4] to Int32 while retaining the binary value of the byte array
"Alan" <alan@surfbest.net> wrote in message
news:10sehpr7k08bkc5@news.supernews.com...[color=blue]
>
> "Victor Bazarov" <v.Abazarov@comAcast.net> wrote in message
> news:CfHxd.12489$Ae.5353@newsread1.dllstx09.us.to. verio.net...[color=green]
>> Alan wrote:[color=darkred]
>> > <jeff@foundrymusic.com> wrote in message
>> > news:1103573749.208215.95260@f14g2000cwb.googlegro 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)[/color]
>>
>> What's a 'union' outside of C++ context? You are contradicting yourself.[/color]
>
> Hardly, they are also used in other languages (as you know), e.g. C
>[color=green]
>> If the OP has to look at the union, the question has _everything_ to do
>> with C++.[/color]
>
> see above
>[color=green]
>> Besides, unions are not for conversions between types.[/color]
>
> ..but they can be used for that in C
>
>[/color]
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 | | | | re: convert form byte[4] to Int32 while retaining the binary value of the byte array
Victor Bazarov wrote:[color=blue]
> Jonathan Mcdougall wrote:[color=green]
>> Int32 convert(Byte b[4])
>> {
>> return (b[0] << 24) | (b[1] << 16) | (b[2] << 8) | (b[3]);
>> }
>>
>> Byte b[4] = { 1, 2, 3, 4 };
>>
>> Int32 i = convert(b);
>>
>>Let's not get _that_ picky, people.[/color]
>
>
> How did you know the OP didn't want
>
> return (b[3] << 24) | (b[2] << 16) | (b[1] << 8) | (b[0]);[/color]
I don't, but I think my answer (and the one you just gave) helped the OP
quite more than "First of all, you have to be a bit more specific."
But I may be wrong.
Jonathan | | | | re: convert form byte[4] to Int32 while retaining the binary value of the byte array
Jonathan Mcdougall wrote:[color=blue]
> Victor Bazarov wrote:
>[color=green]
>> Jonathan Mcdougall wrote:
>>[color=darkred]
>>> Int32 convert(Byte b[4])
>>> {
>>> return (b[0] << 24) | (b[1] << 16) | (b[2] << 8) | (b[3]);
>>> }
>>>
>>> Byte b[4] = { 1, 2, 3, 4 };
>>>
>>> Int32 i = convert(b);
>>>
>>> Let's not get _that_ picky, people.[/color]
>>
>>
>>
>> How did you know the OP didn't want
>>
>> return (b[3] << 24) | (b[2] << 16) | (b[1] << 8) | (b[0]);[/color]
>
>
> I don't, but I think my answer (and the one you just gave) helped the OP
> quite more than "First of all, you have to be a bit more specific."
>
> But I may be wrong.[/color]
You may be. Only the OP can say.
V | | | | re: convert form byte[4] to Int32 while retaining the binary value of the byte array
"Alan" <alan@surfbest.net> skrev i en meddelelse
news:10sehpr7k08bkc5@news.supernews.com...[color=blue]
>
> "Victor Bazarov" <v.Abazarov@comAcast.net> wrote in message
> news:CfHxd.12489$Ae.5353@newsread1.dllstx09.us.to. verio.net...[color=green]
>> Alan wrote:[color=darkred]
>> > <jeff@foundrymusic.com> wrote in message
>> > news:1103573749.208215.95260@f14g2000cwb.googlegro 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)[/color]
>>
>> What's a 'union' outside of C++ context? You are contradicting yourself.[/color]
>
> Hardly, they are also used in other languages (as you know), e.g. C
>[color=green]
>> If the OP has to look at the union, the question has _everything_ to do
>> with C++.[/color]
>
> see above
>[color=green]
>> Besides, unions are not for conversions between types.[/color]
>
> ..but they can be used for that in C[/color]
But not portably in C++.
union converter
{
byte b[4];
int32 i;
};
....
converter bomb;
bomb.b[0] = ...;
....
bomb.b[3] = ...;
int32 blow = bomb.i;
The last statement causes undefined behaviour.
[color=blue]
>
>[/color] | | | | re: convert form byte[4] to Int32 while retaining the binary value of the byte array
"Howard" <alicebt@hotmail.com> skrev i en meddelelse
news:yJGxd.13993$uM5.8474@bgtnsc05-news.ops.worldnet.att.net...[color=blue]
>
> <jeff@foundrymusic.com> wrote in message
> news:1103573749.208215.95260@f14g2000cwb.googlegro ups.com...[color=green]
>> how do you convert form byte[4] to Int32 while retaining the binary
>> value of the byte array
>>[/color]
>
> 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
>[/color]
Also the above code gives undefined behaviour (alignment issues)
/Peter | | | | re: convert form byte[4] to Int32 while retaining the binary value of the byte array jeff@foundrymusic.com wrote:
[color=blue]
> how do you convert form byte[4] to Int32 while retaining the binary
> value of the byte array[/color]
Off topic in here.
Now if you are talking about CLI/.NET, that is you are talking about
System::Int32 (which maps to int and long anyway), System::Byte (which
maps to unsigned char and can also map to char), you can't cast it from
one type to another.
What you can do is to copy each value to a new array of the preferred type.
--
Ioannis Vranos http://www23.brinkster.com/noicys | | | | re: convert form byte[4] to Int32 while retaining the binary value of the byte array
Victor Bazarov wrote:[color=blue]
> Jonathan Mcdougall wrote:
>[color=green]
>>Victor Bazarov wrote:
>>
>>[color=darkred]
>>>Jonathan Mcdougall wrote:
>>>
>>>
>>>>Int32 convert(Byte b[4])
>>>>{
>>>> return (b[0] << 24) | (b[1] << 16) | (b[2] << 8) | (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] << 8) | (b[0]);[/color]
>>
>>
>>I don't, but I think my answer (and the one you just gave) helped the OP
>>quite more than "First of all, you have to be a bit more specific."
>>
>>But I may be wrong.[/color]
>
>
> You may be. Only the OP can say.[/color]
And he don't seem to care very much.
By the way, I said that more as a joke, I didn't mean to offense anyone.
Happy holidays anyways.
Jonathan | | | | re: convert form byte[4] to Int32 while retaining the binary value of the byte array
"Howard" <alicebt@hotmail.com> wrote in message news:dYHxd.14210$uM5.10147@bgtnsc05-news.ops.worldnet.att.net...[color=blue]
>
> "Alan" <alan@surfbest.net> wrote in message
> news:10sehpr7k08bkc5@news.supernews.com...[color=green]
> >
> > "Victor Bazarov" <v.Abazarov@comAcast.net> wrote in message
> > news:CfHxd.12489$Ae.5353@newsread1.dllstx09.us.to. verio.net...[color=darkred]
> >> Alan wrote:
> >> > <jeff@foundrymusic.com> wrote in message
> >> > news:1103573749.208215.95260@f14g2000cwb.googlegro 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.[/color]
> >
> > Hardly, they are also used in other languages (as you know), e.g. C
> >[color=darkred]
> >> If the OP has to look at the union, the question has _everything_ to do
> >> with C++.[/color]
> >
> > see above
> >[color=darkred]
> >> Besides, unions are not for conversions between types.[/color]
> >
> > ..but they can be used for that in C
> >
> >[/color]
>
> 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? :-)[/color]
Howard, you're certainly confused but it is more the mysterious wandering
of you mind than anything that I wrote. It's unfortunate that these personal
attacks have apparently kept the op away, and that we'll probably never know
what he was trying to do.
My "hint" of using a union, was just a suggestion not a solution. He could load
a 32-bit value into such a union and examine the byte array. Maybe it would
tell him what he wants to know. To assume that that the op, Jeff, was seeking
a C++ solution is an assumption on your part, as many people post here with
questions quite unrelated to C++, as you well know.
Jeff, just in case you're reading this, try using a union. If you wanted a solution
in a C++ context let me apologise for the "Your question has nothing to do
with C++."
-Alan | | | | re: convert form byte[4] to Int32 while retaining the binary value of the byte array
Alan wrote:[color=blue]
> My "hint" of using a union, was just a suggestion not a solution.[/color]
That was a bad suggestion.
[color=blue]
> He could load
> a 32-bit value into such a union and examine the byte array.[/color]
That's not portable. There are other ways to do that.
[color=blue]
> Maybe it would
> tell him what he wants to know. To assume that that the op, Jeff, was seeking
> a C++ solution is an assumption on your part, as many people post here with
> questions quite unrelated to C++, as you well know.[/color]
The thing is, that's a c++ newsgroup. We therefore assume the language
is C++.
[color=blue]
> Jeff, just in case you're reading this, try using a union.[/color]
No! Please, read about the problems associated with this method.
Jonathan | | | | re: convert form byte[4] to Int32 while retaining the binary value of the byte array
"Jonathan Mcdougall" <jonathanmcdougall@DELyahoo.ca> wrote in message news:k9%xd.28679$oN3.725509@weber.videotron.net...[color=blue]
> Alan wrote:[color=green]
> > My "hint" of using a union, was just a suggestion not a solution.[/color]
>
> That was a bad suggestion.
>[color=green]
> > He could load
> > a 32-bit value into such a union and examine the byte array.[/color]
>
> That's not portable. There are other ways to do that.
>[color=green]
> > Maybe it would
> > tell him what he wants to know. To assume that that the op, Jeff, was seeking
> > a C++ solution is an assumption on your part, as many people post here with
> > questions quite unrelated to C++, as you well know.[/color]
>
> The thing is, that's a c++ newsgroup. We therefore assume the language
> is C++.
>[color=green]
> > Jeff, just in case you're reading this, try using a union.[/color]
>
> No! Please, read about the problems associated with this method.[/color]
No problems here, it seems:
#include <iostream>
using namespace std;
typedef unsigned char byte;
typedef unsigned long int32;
union converter {
byte b[4];
int32 i;
};
int main() {
converter c;
c.b[3] = (byte) 0x12;
c.b[2] = (byte) 0x34;
c.b[1] = (byte) 0x56;
c.b[0] = (byte) 0x78;
int32 r = c.i;
cout << "r = 0x" << hex << r << endl;
cout << "b = 0x" << hex << (int32)c.b[3]
<< (int32)c.b[2]
<< (int32)c.b[1]
<< (int32)c.b[0] << endl;
return 0;
}
// results
// r = 0x12345678
// b = 0x12345678 |  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,419 network members.
|