難道有人請幫助我關於浮點變量的字節順序嗎?實際上,代碼在Solaris上正常工作,但不在Windows Xp上。這裏是我的代碼一塊例如: ....如何將浮點數轉換爲無符號變量?
int x_snd=1;
float y_snd=1.13;
struct {
int xx_snd;
float yy_snd;
} data_snd;
int x_rec;
float y_rec;
struct {
int xx_rec;
float yy_rec;
} data_rec;
//marshalling
data_snd.xx_snd=htons(x_snd);
data_snd.yy_snd=htonl(*(int*) &y_snd);
//write data to socket
send(sock1, &data_snd, ...
//clean ...
//read data from socket
if recv(sock, &data_rec ...
//unmarshalling
x_rec=ntohs(data_rec.xx_rec);
y_rec= *(float*) &(ntohl(data_rec.yy_rec));
...
釷代碼是用gcc上的Wndows編譯在Unix和與MSVC++ 6。 您的幫助將是非常讚賞,我會很高興,如果你能指點我,讓字節排列順序有用信息的任何鏈接或文件...
請多關照您的幫助, MK
感謝您的幫助 您能給我說說第二個更多資料你描述?任何描述這種FLT_RADIX值的鏈接都將非常感謝!再次 感謝, MK – make 2009-11-12 01:10:46
嗯,我給你提供的代碼(我只寫我自己,所以我沒有任何鏈接指向你)。如果您對Sign-Mantissa-Exponent表示浮點數的方式進行了一些研究,應該清楚代碼的作用。 – caf 2009-11-12 01:19:05
感謝您的幫助。 請糾正我,如果這是正確的! int x_snd = 1; float y_snd = 1.13; // int y_snd_exp; long y_snd_mant; struct { \t int xx_snd; \t float yy_snd; data_snd; // marshalling y_snd_exp = ilogbf(y_snd); y_snd_mant =(scalbnf(fabsf(y_snd),-y_snd_exp) - 1)* LONG_MAX; if(y_snd <0.0)y_snd_mant = -y_snd_mant; 012_sl.xx_snd = htons(x_snd); data_snd.yy_snd = htonl(y_snd_mant); data_snd.yy_snd = htonl(y_snd_mant); //將數據寫入到插座 發送(sock1,&data_snd,... ... – make 2009-11-12 02:22:13