求IEEE转换成IBM浮点数代码

来源:百度知道 编辑:UC知道 时间:2024/05/18 02:26:16
刚接触石油软件开发,这种转换不会啊。公司有份代码,看懂,这份代码也不是公司人写的。
大家看看能否看懂.
void segy2float(char* from, char* to,int len, int type)
{
unsigned fconv, fsign, fexpn;
register unsigned fmant;
register int t;
register char *bp = (char *) &fconv;
double tmp;
if (len <= 0) return;
switch(type)
{
case 2: /* IEEE to IBM */ case 1: /* IBM to IEEE */
while (len--)
{
/* Load. Swap bytes if necessary */
#ifdef IS_LITTLE_ENDIAN
bp[0] = from[0];
bp[1] = from[1];
bp[2] = from[2];
bp[3] = from[3];
#else
bp[0] = from[3];
bp[1] = from[2];
bp[2] = from[1];
bp[3] = from[0];
#endif
from += 4;
/** 取符号位 */
fsign = (unsigned) 0x80000000 & fconv;
/** 从第一个字节中的后7位取指数 */
fexpn = (unsigned) 0x7f000000 & fconv;
/** 取尾数 */
fmant = (unsigned) 0x00ffffff & fconv;
if (!

/** 取符号位 */
fsign = (unsigned) 0x80000000 & fconv;
/** 从第一个字节中的后7位取指数 */
fexpn = (unsigned) 0x7f000000 & fconv;
/** 取尾数 */
fmant = (unsigned) 0x00ffffff & fconv;

这个地方是否合适?32位浮点的格式的第一位为符号位,接下来的8位为指数位,后面的23位为尾数位。

他这种取法,取了符号位没做偏移,得到的是0x10000000或0x00000000;指数位漏掉了最后一位;尾数位又取到了指数位的最后一位。