G
GeLLcheN
Hello everybody,
I've got a little (endian) problem.
I'm programming a network-based application, in which I'll use structs,
interpreting ethernet packets.
My intention is to read the stream directly and transport to the right
struct. But this seems to be not so simple:
Here's a sample code demonstrating my problem:
----------------------------------------------------------------------------------------------------------------
#include <iostream>
using namespace std;
typedef unsigned short WORD;
typedef struct {
WORD address;
/*unsigned test1: 4;
unsigned test2: 4;*/
unsigned char aha;
unsigned test3: 4;
unsigned test4: 4;
} testStruct;
int main(int argc, char* argv[])
{
unsigned char aha[5]={0x12, 0x34, 0xAB, 0xCD, 0xEF};
testStruct structure;
testStruct * pStructure;
pStructure=(testStruct*)&aha;
structure=*pStructure;
return 0;
}
------------------------------------------------------------------------------------------------------------
My debugger shows:
- for word address 0x3412 correct!
- for char aha 0xAB
BUT:
- unsigned 4 test3: 0x0000000F
- unsigned 4 test4: 0x0000000E
When i use test1 and test2 (in comment brackets) it also seems to be
buggy!
It would be nice if somebody could give me a simple idea so that I
could resolve this situation.
I would be very thankful!
Regards,
JPMR
I've got a little (endian) problem.
I'm programming a network-based application, in which I'll use structs,
interpreting ethernet packets.
My intention is to read the stream directly and transport to the right
struct. But this seems to be not so simple:
Here's a sample code demonstrating my problem:
----------------------------------------------------------------------------------------------------------------
#include <iostream>
using namespace std;
typedef unsigned short WORD;
typedef struct {
WORD address;
/*unsigned test1: 4;
unsigned test2: 4;*/
unsigned char aha;
unsigned test3: 4;
unsigned test4: 4;
} testStruct;
int main(int argc, char* argv[])
{
unsigned char aha[5]={0x12, 0x34, 0xAB, 0xCD, 0xEF};
testStruct structure;
testStruct * pStructure;
pStructure=(testStruct*)&aha;
structure=*pStructure;
return 0;
}
------------------------------------------------------------------------------------------------------------
My debugger shows:
- for word address 0x3412 correct!
- for char aha 0xAB
BUT:
- unsigned 4 test3: 0x0000000F
- unsigned 4 test4: 0x0000000E
When i use test1 and test2 (in comment brackets) it also seems to be
buggy!
It would be nice if somebody could give me a simple idea so that I
could resolve this situation.
I would be very thankful!
Regards,
JPMR