Convert the raw data in little endian format on x64 system

The raw data gets from the device in endian neutral format. I want to build a common subroutine to convert it in little endain before to copy data out in a structure (don't care how the structure is defined).

For example, a 16-byte of data from the device

Byte_0 _1 _2 _3 _4 _5 _6 _7 _8 _9 _A _B _C _D _E _F

-

Data00 01 00 01 10 00 00 17 00 10 00 01 01 00 00 02

is converted in this order of little endian.

Byte_3 _2 _1 _0 _7 _6 _5 _4 _B _A _9 _8 _F _E _D _C

-

Data01 00 01 00 17 00 00 10 01 00 10 00 02 00 00 01

If the structure is defined as the following:

struct {

short A;/* Byte 0-1 */

short B;/* Byte 2-3 */

short C;/* Byte 4-5 */

short D;/* Byte 6-7 */

short E;/* Byte 8-9 */

short F;/* Byte A-B */

short G; /* Byte C-D */

short H; /* Byte E-F */

}

The printout is

Short A ... 0x1/* Byte 2-3 */

Short B ... 0x1/* Byte 0-1 */

Short C ... 0x 17/* Byte 6-7 */

Short D ... 0x1000 /* Byte 4-5 */

Short E ... 0x1/* Byte A-B */

Short F ... 0x 10/* Byte 8-9 */

Short G ... 0x2/* Byte E-F */

Short H ... 0x 100 /* Byte C-D */

The source code is post here.

void byte_convert (char *data, uint size)

{

int i=0, j;

char *tmp;

(void) memset (tmp, 0, size);

for (j=0; j<size; j=j+4) {

switch(size%4) {

case 0:

*(tmp+i)= *(data+i+3);

*(tmp+i+1) = *(data+i+2);

*(tmp+i+2) = *(data+i+1);

*(tmp+i+3) = *(data+i);

i = i + 4;

break;

case 1:

*(tmp+i)= *(data+i);

i = i + 1;

break;

case 2:

*(tmp+i)= *(data(i+1);

*(tmp+i+1) = *(data(i);

i = i + 2;

break;

case 3:

*(tmp+i)= *(data+i+2);

*(tmp+i+1) = *(data+i+1);

*(tmp+i+2) = *(data+i);

i = i + 3;

break;

}

}

bcopy(tmp, data, size);

}

return;

}

How is the data located in the memory on Solaris x64 system? What is the wrong in my code?>

[2117 byte] By [jeffli] at [2007-11-26 10:45:49]
# 1

First of all, there are a number of syntax errors in your example.

Secondly, in this statement

(void) memset (tmp, 0, size);

tmp is left uninitialized.

Also, there are several things in your code that I do not understand: for example, why switch control expression is "size%4"? You probably meant "j%4".

With all the above errors fixed, your code should work.

MaximKartashev at 2007-7-7 2:57:55 > top of Java-index,Development Tools,Solaris and Linux Development Tools...