Is there any standard method in java to convert IBM 370 (in byte form) to IEEE format. "Any conversion algorithm would help.
I tried writing Java code. But I do not understand where I am mistaken. When I give input as -2.000000000000000E + 02, I get the value as -140.0 in IEEE format. and in another case, when I give input as 3.140000000000000E + 00, I get the value as 3.1712502374909226 in IEEE format. Any help on this would be greatly 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; }
source share