I send a float equal to 4.000 from Windows to an Amazon EC2 Linux server, but the server reads it as 8.47522e+11.[/font]
[color=#000000][font=verdana, arial, sans-serif]
The server sends back 4.000 but Windows reads it as 0.0.[/font]
[color=#000000][font=verdana, arial, sans-serif]
I tried changing the byte order using this on Windows:[/font]
[color=#000000][font=verdana, arial, sans-serif]
[background=rgb(242, 242, 242)]void ReverseFloat(float* f)
{
unsigned char* uc = (unsigned char*)f;
unsigned char temp = uc[0];
uc[0] = uc[3];
uc[3] = temp;
temp = uc[1];
uc[1] = uc[2];
uc[2] = temp;
}
[/background][/font]
[color=#000000][font=verdana, arial, sans-serif]
{
unsigned char* uc = (unsigned char*)f;
unsigned char temp = uc[0];
uc[0] = uc[3];
uc[3] = temp;
temp = uc[1];
uc[1] = uc[2];
uc[2] = temp;
}
But the server now gets 8.47518e+11 and Windows gets 0.0.[/font]
[color=#000000][font=verdana, arial, sans-serif]
The struct is packed and it still gives me the wrong result.[/font]
[color=#000000][font=verdana, arial, sans-serif]
[background=rgb(242, 242, 242)]#if !defined( _SERVER )
#pragma pack(push, 1)
#endif
typedef struct
{
unsigned char type;
float version;
char username[16];
char password[16];
}
#if defined( _SERVER )
__attribute__ ((packed))
#endif
LoginPacket;
#if !defined( _SERVER )
#pragma pack(pop)
#endif
[/background][/font]
The client sends 37 bytes and server receives 37 bytes.
The server sends back this:
#if !defined( _SERVER )
#pragma pack(push, 1)
#endif
typedef struct
{
unsigned char type;
float version;
}
#if defined( _SERVER )
__attribute__ ((packed))
#endif
WrongVersionPacket;
#if !defined( _SERVER )
#pragma pack(pop)
#endif
The server says it sends 8 bytes for some reason and the client receives 8. I don't know why it's 8... should be 5. Is a float 7 bytes on Amazon Linux?
#pragma pack(push, 1)
#endif
typedef struct
{
unsigned char type;
float version;
char username[16];
char password[16];
}
#if defined( _SERVER )
__attribute__ ((packed))
#endif
LoginPacket;
#if !defined( _SERVER )
#pragma pack(pop)
#endif
#pragma pack(push, 1)
#endif
typedef struct
{
unsigned char type;
float version;
}
#if defined( _SERVER )
__attribute__ ((packed))
#endif
WrongVersionPacket;
#if !defined( _SERVER )
#pragma pack(pop)
#endif