IBM到IEEE浮点转换 [英] IBM to IEEE floating point conv

查看:244
本文介绍了IBM到IEEE浮点转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在java中有没有标准的方法来将IBM 370(以字节的形式)转换为IEEE格式。转换的任何算法都会有帮助..

我试图写一个Java代码..但我不明白我在哪里出错。当我给输入-2.000000000000000E + 02,我得到的值为-140.0在IEEE格式。并在其他情况下,当我给输入为3.140000000000000E + 00我得到的价值为3.1712502374909226 IEEE格式任何帮助,将不胜感激

  private void conversion(){
byte [] buffer = //要读取的字节(8字节);
int sign =(buffer [0]& 0x80);
//提取指数。
int exp =((buffer [0]& 0x7f) - 64)* 4 - 1;
//标准化尾数。 ((缓冲区[1]& 0x80)== 0){$ b $($ i = 0; i< 4; i ++) b buffer = leftShift(buffer);
exp = exp - 1;



//把符号和尾数换成8个字节的数字
buffer = rightShift(buffer); //为更长的指数腾出空间
buffer = rightShift(buffer);
buffer = rightShift(buffer);
exp = exp + 1023; //过量的1023格式
int temp = exp& 0x0f; //低4位进入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 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;


解决方案

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


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;
}

解决方案

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

这篇关于IBM到IEEE浮点转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆