J
jeff
how do you convert form byte[4] to Int32 while retaining the binary
value of the byte array
value of the byte array
how do you 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
Howard said: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:
Jonathan said:Howard said: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] << 8) | (b[3]);
}
Byte b[4] = { 1, 2, 3, 4 };
Int32 i = convert(b);
Let's not get _that_ picky, people.
Jonathan Mcdougall said:Howard said: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] << 8) | (b[3]);
}
Byte b[4] = { 1, 2, 3, 4 };
Int32 i = convert(b);
Let's not get _that_ picky, people.
how do you convert form byte[4] to Int32 while retaining the binary
value of the byte array
Alan said: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)
Victor Bazarov said:Alan said: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.
Alan said:Victor Bazarov said:Alan said: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
Victor said:Jonathan said: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]);
Jonathan said:Victor said:Jonathan said: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]);
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.
Alan said:Victor Bazarov said:Alan said: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
Also the above code gives undefined behaviour (alignment issues)Howard said: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
how do you convert form byte[4] to Int32 while retaining the binary
value of the byte array
Victor said:Jonathan said:Victor Bazarov wrote:
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]);
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.
You may be. Only the OP can say.
Howard said:Alan said:Victor Bazarov said:Alan wrote:
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?
Alan said: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.
Jonathan Mcdougall said:That was a bad suggestion.
That's not portable. There are other ways to do that.
The thing is, that's a c++ newsgroup. We therefore assume the language
is C++.
No! Please, read about the problems associated with this method.
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.