从IBM浮点转换为IEEE浮点标准和反向Versa In C#?

aiw*_*aiw 5 c c# floating-point ieee-754

正在为我们正在使用的旧系统寻找IEEE浮点数到IBM浮点格式的方法.

我们可以在C#中使用通用公式吗?

spe*_*ing 6

// http://en.wikipedia.org/wiki/IBM_Floating_Point_Architecture
// float2ibm(-118.625F) == 0xC276A000
// 1 100 0010    0111 0110 1010 0000 0000 0000
// IBM/370 single precision, 4 bytes
// xxxx.xxxx xxxx.xxxx xxxx.xxxx xxxx.xxxx
// s|-exp--| |--------fraction-----------|
//    (7)          (24)
// value = (-1)**s * 16**(e - 64) * .f   range = 5E-79 ... 7E+75
static int float2ibm(float from)
{
    byte[] bytes = BitConverter.GetBytes(from);
    int fconv = (bytes[3] << 24) | (bytes[2] << 16) | (bytes[1] << 8)| bytes[0];

    if (fconv == 0) return 0;
    int fmant = (0x007fffff & fconv) | 0x00800000;
    int t = (int)((0x7f800000 & fconv) >> 23) - 126;
    while (0 != (t & 0x3)) { ++t; fmant >>= 1; }
    fconv = (int)(0x80000000 & fconv) | (((t >> 2) + 64) << 24) | fmant;
    return fconv; // big endian order
}
Run Code Online (Sandbox Code Playgroud)

我更改了一段称为static void float_to_ibm(int from [],int to [],int n,int endian)的代码,上面的代码可以 PC 以小端浮点数开始正确运行 。 返回值是大端序ibm浮点数,但存储为int类型。


Dav*_*nan 2

一种明显的方法是使用数字的文本表示作为交换格式。