Re: Endian revisited




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

.



Relevant Pages

  • Re: "Sorting" assignment
    ... you think using memcpy a block at a time is not the best way to swap ... trouble now or later. ... It does not bother to zero the array (you'd need a clever optimiser ... void memswap(char *ptrToA, char *ptrToB, int intLength) ...
    (comp.programming)
  • Re: Using VLOOKUP in an array
    ... I'm quite sure SUMPRODUCT is what you're looking for. ... > "Returns" is a data table for Vlookup. ... > array so that I can do multiple lookups in the same formula. ... >> Robert A wrote: ...
    (microsoft.public.excel)
  • Re: qsort and arbitrary types
    ... >> or swap elements of the type pointed to. ... > an array of arbitrary type, ... > a reference to a comparison function. ...
    (comp.lang.fortran)
  • Re: Using VLOOKUP in an array
    ... Robert A wrote: ... >>> "Returns" is a data table for Vlookup. ... >>> array so that I can do multiple lookups in the same formula. ... My array formula looks up one or more ...
    (microsoft.public.excel)
  • Re: User Defined type()
    ... Swap indices when sorting, not element values. ... keep a separate array of indices and swap them around as you sort ...
    (microsoft.public.vb.general.discussion)