Unix to WIndows Message
-
My Windows MFC Application will receive a structure from a Unix machine (Big Endian) over UDP. The structure has the following data types:-
struct RXData {
double timeStamp;
double item1;
long item2;
double item3;
int flag1;
bool state;
};What do I need to do on the Windows platform to get the data in the correct format; i.e. do I use procedures like theses below - I did try but still not getting the correct answer.
float swap(float d)
{
float a;
unsigned char *dst = (unsigned char *)&a;
insigned char *src = (unsigned char *)&d;
dst[0] = src[3];
dst[1] = src[2];
dst[2] = src[1];
dst[3] = src[0];
return a;
}short convert_short(short in)
{
short out;
char *p_in = (char *) ∈
char *p_out = (char *) &out;
p_out[0] = p_in[1];
p_out[1] = p_in[0];
return out;
}The floats give incorrect values. Any suggestions for this simple scheme please.:confused: Regards, Andy.
-
My Windows MFC Application will receive a structure from a Unix machine (Big Endian) over UDP. The structure has the following data types:-
struct RXData {
double timeStamp;
double item1;
long item2;
double item3;
int flag1;
bool state;
};What do I need to do on the Windows platform to get the data in the correct format; i.e. do I use procedures like theses below - I did try but still not getting the correct answer.
float swap(float d)
{
float a;
unsigned char *dst = (unsigned char *)&a;
insigned char *src = (unsigned char *)&d;
dst[0] = src[3];
dst[1] = src[2];
dst[2] = src[1];
dst[3] = src[0];
return a;
}short convert_short(short in)
{
short out;
char *p_in = (char *) ∈
char *p_out = (char *) &out;
p_out[0] = p_in[1];
p_out[1] = p_in[0];
return out;
}The floats give incorrect values. Any suggestions for this simple scheme please.:confused: Regards, Andy.
Actually you need to convert
double
s, notfloat
s (i.e.8-byte
values instead of4-byte
ones). :)If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
My Windows MFC Application will receive a structure from a Unix machine (Big Endian) over UDP. The structure has the following data types:-
struct RXData {
double timeStamp;
double item1;
long item2;
double item3;
int flag1;
bool state;
};What do I need to do on the Windows platform to get the data in the correct format; i.e. do I use procedures like theses below - I did try but still not getting the correct answer.
float swap(float d)
{
float a;
unsigned char *dst = (unsigned char *)&a;
insigned char *src = (unsigned char *)&d;
dst[0] = src[3];
dst[1] = src[2];
dst[2] = src[1];
dst[3] = src[0];
return a;
}short convert_short(short in)
{
short out;
char *p_in = (char *) ∈
char *p_out = (char *) &out;
p_out[0] = p_in[1];
p_out[1] = p_in[0];
return out;
}The floats give incorrect values. Any suggestions for this simple scheme please.:confused: Regards, Andy.
you said the struct holds doubles, yet you show a swap function dealing with floats, not doubles? apart from that, everything looks OK. Although there are other schemes, some involving a union. :)
Luc Pattyn [Forum Guidelines] [My Articles]
The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.
-
you said the struct holds doubles, yet you show a swap function dealing with floats, not doubles? apart from that, everything looks OK. Although there are other schemes, some involving a union. :)
Luc Pattyn [Forum Guidelines] [My Articles]
The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.
Luc Pattyn wrote:
Although there are other schemes, some involving a union.
Which I would normally opt for. :)
It is a crappy thing, but it's life -^ Carlo Pallini
-
Luc Pattyn wrote:
Although there are other schemes, some involving a union.
Which I would normally opt for. :)
It is a crappy thing, but it's life -^ Carlo Pallini
I cannot find a 'useful usage' of a
union
to reverse byte order (while I highly appreciate unions as 'variants'). :)If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles]