D
DaSt
Hi everybody,
I'm trying to build an test-app to check out typecasting.
I want to typecast from a bunch of u_chars (unsigned chars) to an structure
i created.
Here's the code:
////////////////////////////////////////////////////////////////////////////////////
#include <iostream>
using namespace std;
typedef unsigned char u_char;
typedef unsigned short u_short;
//
// Address structure's
// Layer 2: Datalink
//
//
// struct macaddress
//
struct mac_address
{
u_char bytes[6];
};
//
// Frame structures
// Layer 2: Datalink
//
struct frame_ethernet
{
mac_address destination_mac;
mac_address source_mac;
u_short ethertype;
};
int main ()
{
u_char freth[14];
freth[0] = 0x01;
freth[1] = 0x02;
freth[2] = 0x03;
freth[3] = 0x04;
freth[4] = 0x05;
freth[5] = 0x06;
freth[6] = 0x07;
freth[7] = 0x08;
freth[8] = 0x09;
freth[9] = 0x10;
freth[10] = 0x11;
freth[11] = 0x12;
freth[12] = 0x13;
freth[13] = 0x14;
frame_ethernet* headers_frame_ethernet;
headers_frame_ethernet = (frame_ethernet*) freth;
char ethertype[10];
sprintf (ethertype, "%#x", headers_frame_ethernet->ethertype);
cout << "Class-value: " << (int) headers_frame_ethernet->ethertype << endl;
cout << "True value: " << 0x1314 << endl;
cout << "Hex-value in string: " << ethertype << endl;
system ("pause");
return 0;
}
////////////////////////////////////////////////////////////////////////////////////
The first 12 bytes of the freth var are perfictley converted to the
headers_frame_ethernet structure. But the last 2-bytes, which should go into
the u_short ethertype, are inserted in the wrong way. It should contain the
decimal number 4884 (0x1314), but it contains the number 5139 (0x1413). It
put's the bytes in the wrong order.
I tried this with the GNU compiler, and with the MS-Visual C++ 6 compiler,
and they both turn the wrong way, so i guess i'm doing something wrong...
Who can help me out?
Thnx in advance,
DaSt
I'm trying to build an test-app to check out typecasting.
I want to typecast from a bunch of u_chars (unsigned chars) to an structure
i created.
Here's the code:
////////////////////////////////////////////////////////////////////////////////////
#include <iostream>
using namespace std;
typedef unsigned char u_char;
typedef unsigned short u_short;
//
// Address structure's
// Layer 2: Datalink
//
//
// struct macaddress
//
struct mac_address
{
u_char bytes[6];
};
//
// Frame structures
// Layer 2: Datalink
//
struct frame_ethernet
{
mac_address destination_mac;
mac_address source_mac;
u_short ethertype;
};
int main ()
{
u_char freth[14];
freth[0] = 0x01;
freth[1] = 0x02;
freth[2] = 0x03;
freth[3] = 0x04;
freth[4] = 0x05;
freth[5] = 0x06;
freth[6] = 0x07;
freth[7] = 0x08;
freth[8] = 0x09;
freth[9] = 0x10;
freth[10] = 0x11;
freth[11] = 0x12;
freth[12] = 0x13;
freth[13] = 0x14;
frame_ethernet* headers_frame_ethernet;
headers_frame_ethernet = (frame_ethernet*) freth;
char ethertype[10];
sprintf (ethertype, "%#x", headers_frame_ethernet->ethertype);
cout << "Class-value: " << (int) headers_frame_ethernet->ethertype << endl;
cout << "True value: " << 0x1314 << endl;
cout << "Hex-value in string: " << ethertype << endl;
system ("pause");
return 0;
}
////////////////////////////////////////////////////////////////////////////////////
The first 12 bytes of the freth var are perfictley converted to the
headers_frame_ethernet structure. But the last 2-bytes, which should go into
the u_short ethertype, are inserted in the wrong way. It should contain the
decimal number 4884 (0x1314), but it contains the number 5139 (0x1413). It
put's the bytes in the wrong order.
I tried this with the GNU compiler, and with the MS-Visual C++ 6 compiler,
and they both turn the wrong way, so i guess i'm doing something wrong...
Who can help me out?
Thnx in advance,
DaSt