Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IBM to IEEE floating point conv

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;
}
like image 876
Vineet Avatar asked Jun 18 '11 23:06

Vineet


People also ask

How do you convert a binary number to a floating point?

Converting to Floating pointSet the sign bit - if the number is positive, set the sign bit to 0. If the number is negative, set it to 1. Divide your number into two sections - the whole number part and the fraction part.

How do you convert to floating point representation?

To convert a decimal number to binary floating point representation: Convert the absolute value of the decimal number to a binary integer plus a binary fraction. Normalize the number in binary scientific notation to obtain m and e. Set s=0 for a positive number and s=1 for a negative number.

What is 32 bit IEEE floating?

In the 32 bit IEEE format, 1 bit is allocated as the sign bit, the next 8 bits are allocated as the exponent field, and the last 23 bits are the fractional parts of the normalized number. Sign Exponent Fraction 0 00000000 00000000000000000000000 Bit 31 [30 - 23] [22 - 0]


2 Answers

I can see a couple different solutions to your question:

  • Use the text representation as an intermediary reference
  • Do a straight conversion C code
like image 142
RHSeeger Avatar answered Sep 22 '22 15:09

RHSeeger


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

like image 34
Stephen C Avatar answered Sep 22 '22 15:09

Stephen C