Re: Endian revisited
- From: "Sheldon" <shejo284@xxxxxxxxx>
- Date: 23 Oct 2006 06:43:58 -0700
Robert Latest skrev:
On 23 Oct 2006 05:38:42 -0700,
Sheldon <shejo284@xxxxxxxxx> wrote
in Msg. <1161607121.983189.218700@xxxxxxxxxxxxxxxxxxxxxxxxxxxx>
unsigned char j[4];
uint32_t o;
read(descriptor, j, 4); /* error checking ommitted for simplicity */
o = j[0]; o<<=8;
o | = j[1]; o<<=8;
o | = j[2]; o<<=8;
o | =j[3];
Could someone please explain what this person did, or, explain how to
do this byte swap?
Assuming that you know the meaning of the binary-or and the
bit-shift operator, what is it thar you're having trouble with?
In fact the code is written a bit awkwardly; I'd have done it like
this:
o = (j[0] << 24) | (j[1] << 16) | (j[2] << 8) | j[3];
In your case you'd have to do it differently because the bitwise
operators won't make much sense with floats. You'd just swap the
four bytes in your array and cast the result to the appropriate
type -- assuming that the binary representation of floats on the
machine that generated the numbers is the same as on the machine
you read them with.
robert
I cannot check to see if the binary representation on my PC and the IBM
the files were written on are the same but they should be ok since this
conversion was done on my machine before using IDL and Python.
What I don't understand is that four bytes are read in an array and
then shifted: byte 1 to the left by 24, byte 2 by 16, byte 3 by 8 and
then byte 4 remains. What happened? Does this shifting pushes the bytes
forward so that at the end byte 4 is in the first 4 addresses?
I would be reading in 2D array and how then would I do this bytewise
shifting for each position?
/Sheldon
.
- References:
- Endian revisited
- From: Sheldon
- Re: Endian revisited
- From: Robert Latest
- Endian revisited
- Prev by Date: Re: Endian revisited
- Next by Date: POSIX system() call return value
- Previous by thread: Re: Endian revisited
- Next by thread: Re: Endian revisited
- Index(es):
Relevant Pages
|
|