IBM 到 IEEE 浮点转换

发布于 2024-11-16 14:25:57 字数 1666 浏览 2 评论 0原文

java中是否有任何标准方法可以将IBM 370(以字节形式)转换为IEEE格式。任何转换算法都会有所帮助..

我尝试编写java代码..但我不明白我哪里出错了。当我将输入指定为 -2.000000000000000E+02 时,我得到的值为 IEEE 格式的 -140.0。在其他情况下,当我输入 3.140000000000000E+00 时,我得到的值为 IEEE 格式的 3.1712502374909226 对此的任何帮助将不胜感激

private void conversion() {
    byte[] buffer = //bytes to be read(8 bytes);
    int sign = (buffer[0] & 0x80);
    // Extract exponent.
    int exp = ((buffer[0] & 0x7f) - 64) * 4 - 1;
    //Normalize the mantissa.
    for (int i = 0; i < 4; i++) {//since 4 bits per hex digit
        if ((buffer[1] & 0x80) == 0) {
            buffer = leftShift(buffer);
            exp = exp - 1;
        }
    }

    // Put sign and mantissa back in 8-byte number
    buffer = rightShift(buffer);// make room for longer exponent
    buffer = rightShift(buffer);
    buffer = rightShift(buffer);
    exp = exp + 1023;//Excess 1023 format
    int temp = exp & 0x0f;//Low 4 bits go into B(1)
    buffer[1]= (byte)((buffer[1]&0xf) | (temp *16));
    buffer[0]= (byte)(sign | ((exp/16) & 0x7f));
    }

     private byte[] rightShift(byte[] buf) {
    int newCarry = 0;
    int oldCarry = 0;
    for(int i = 1; i<buf.length; i++) {
        newCarry = buf[i] & 1;
        buf[i] = (byte)((buf[i] & 0xFE)/2 + (oldCarry != 0 ? 0x80 : 0));
        oldCarry = newCarry;
    }
    return buf;
}

private byte[] leftShift(byte[] buf) {
    int newCarry = 0;
    int oldCarry = 0;
    for(int i = buf.length-1; i>0; i--) {
        newCarry = buf[i] & 1;
        buf[i] = (byte)((buf[i] & 0x7F)*2 + (oldCarry != 0 ? 1 : 0));
        oldCarry = newCarry;
    }   
    return buf;
}

Is there any standard method in java to convert IBM 370(in the form of bytes) to IEEE format.?Any algorithm for the conversion would help..

I tried writing a java code..But i fail to understand where do i go wrong. When i give the input as -2.000000000000000E+02, i'm getting the value as -140.0 in IEEE format. and in othercase when i give the input as 3.140000000000000E+00 i'm getting the value as 3.1712502374909226 in IEEE format Any help on this would be highly appreciated

private void conversion() {
    byte[] buffer = //bytes to be read(8 bytes);
    int sign = (buffer[0] & 0x80);
    // Extract exponent.
    int exp = ((buffer[0] & 0x7f) - 64) * 4 - 1;
    //Normalize the mantissa.
    for (int i = 0; i < 4; i++) {//since 4 bits per hex digit
        if ((buffer[1] & 0x80) == 0) {
            buffer = leftShift(buffer);
            exp = exp - 1;
        }
    }

    // Put sign and mantissa back in 8-byte number
    buffer = rightShift(buffer);// make room for longer exponent
    buffer = rightShift(buffer);
    buffer = rightShift(buffer);
    exp = exp + 1023;//Excess 1023 format
    int temp = exp & 0x0f;//Low 4 bits go into B(1)
    buffer[1]= (byte)((buffer[1]&0xf) | (temp *16));
    buffer[0]= (byte)(sign | ((exp/16) & 0x7f));
    }

     private byte[] rightShift(byte[] buf) {
    int newCarry = 0;
    int oldCarry = 0;
    for(int i = 1; i<buf.length; i++) {
        newCarry = buf[i] & 1;
        buf[i] = (byte)((buf[i] & 0xFE)/2 + (oldCarry != 0 ? 0x80 : 0));
        oldCarry = newCarry;
    }
    return buf;
}

private byte[] leftShift(byte[] buf) {
    int newCarry = 0;
    int oldCarry = 0;
    for(int i = buf.length-1; i>0; i--) {
        newCarry = buf[i] & 1;
        buf[i] = (byte)((buf[i] & 0x7F)*2 + (oldCarry != 0 ? 1 : 0));
        oldCarry = newCarry;
    }   
    return buf;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

永言不败 2024-11-23 14:25:57

我可以看到您的问题有几种不同的解决方案:

I can see a couple different solutions to your question:

  • Use the text representation as an intermediary reference
  • Do a straight conversion C code
浮生面具三千个 2024-11-23 14:25:57

IBM 技术文章包含从 IBM 浮点格式转换为 IEE 浮点的算法。

This IBM Technical Article includes algorithms for converting from IBM floating point formats to IEE floating point.

与之呼应 2024-11-23 14:25:57

leftShift() 函数中有一个错误,您应该使用 0x80 而不是 1 进行掩码。这是更正后的函数。

    private byte[] leftShift(byte[] buf) {
        int newCarry = 0;
        int oldCarry = 0;
        for(int i = buf.length-1; i>0; i--) {
            newCarry = buf[i] & 0x80;
            buf[i] = (byte)((buf[i] & 0x7F)*2 + (oldCarry != 0 ? 1 : 0));
            oldCarry = newCarry;
        }
        return buf;
    }

我用 wiki 示例进行了测试 -118.625 如果我理解正确的话,IBM 的偏见会加倍也是 64,所以二进制将是 11000010 01110110 10100000 00000000 00000000 00000000 00000000 00000000。修复后,程序可以正确生成-118.625。

我知道这是一个旧帖子,但我目前也遇到了同样的情况。

There is a bug in the leftShift() function, where you should mask with 0x80 instead of 1. Here is the corrected function.

    private byte[] leftShift(byte[] buf) {
        int newCarry = 0;
        int oldCarry = 0;
        for(int i = buf.length-1; i>0; i--) {
            newCarry = buf[i] & 0x80;
            buf[i] = (byte)((buf[i] & 0x7F)*2 + (oldCarry != 0 ? 1 : 0));
            oldCarry = newCarry;
        }
        return buf;
    }

I tested with the wiki example -118.625 If I understand correctly, the bias for IBM double is also 64, so the binary will be 11000010 01110110 10100000 00000000 00000000 00000000 00000000 00000000. After the fix, the program can produce -118.625 correctly.

I know it is an old post, but I currently ran into the same situation too.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文