1 21 package org.apache.derby.client.am; 22 23 27 public class FloatingPoint { 28 private FloatingPoint() { 30 } 31 32 35 public final static int IEEE_754_FLOATING_POINT = 0x48; 36 37 39 42 private static final int convertFromByteToInt(byte[] buffer, int offset) { 43 return (buffer[offset] << 24) | 44 ((buffer[offset + 1] & 0xFF) << 16) | 45 ((buffer[offset + 2] & 0xFF) << 8) | 46 (buffer[offset + 3] & 0xFF); 47 } 48 49 52 private static final long convertFromByteToLong(byte[] buffer, int offset) { 53 return ((buffer[offset] & 0xFFL) << 56) | 54 ((buffer[offset + 1] & 0xFFL) << 48) | 55 ((buffer[offset + 2] & 0xFFL) << 40) | 56 ((buffer[offset + 3] & 0xFFL) << 32) | 57 ((buffer[offset + 4] & 0xFFL) << 24) | 58 ((buffer[offset + 5] & 0xFFL) << 16) | 59 ((buffer[offset + 6] & 0xFFL) << 8) | 60 (buffer[offset + 7] & 0xFFL); 61 } 62 63 64 66 73 public static final float getFloat(byte[] buffer, int offset) { 74 return Float.intBitsToFloat(convertFromByteToInt(buffer, offset)); 75 } 76 77 85 public static final double getDouble(byte[] buffer, int offset) { 86 return Double.longBitsToDouble(convertFromByteToLong(buffer, offset)); 87 } 88 89 91 94 public static final void floatToIeee754Bytes(byte[] buffer, int offset, float f) { 95 int intBits = Float.floatToIntBits(f); 96 buffer[offset] = (byte) ((intBits >>> 24) & 0xFF); 97 buffer[offset + 1] = (byte) ((intBits >>> 16) & 0xFF); 98 buffer[offset + 2] = (byte) ((intBits >>> 8) & 0xFF); 99 buffer[offset + 3] = (byte) (intBits & 0xFF); 100 } 101 102 105 public static final void doubleToIeee754Bytes(byte[] buffer, int offset, double d) { 106 long longBits = Double.doubleToLongBits(d); 107 buffer[offset] = (byte) ((longBits >>> 56) & 0xFF); 108 buffer[offset + 1] = (byte) ((longBits >>> 48) & 0xFF); 109 buffer[offset + 2] = (byte) ((longBits >>> 40) & 0xFF); 110 buffer[offset + 3] = (byte) ((longBits >>> 32) & 0xFF); 111 buffer[offset + 4] = (byte) ((longBits >>> 24) & 0xFF); 112 buffer[offset + 5] = (byte) ((longBits >>> 16) & 0xFF); 113 buffer[offset + 6] = (byte) ((longBits >>> 8) & 0xFF); 114 buffer[offset + 7] = (byte) (longBits & 0xFF); 115 } 116 } 117 | Popular Tags |