7 * @author Bob Hanson hansonr@stolaf.edu
13 // unnecessary to instantialize unless subclassed
16 public static float bytesToFloat(byte[] bytes, int j, boolean isBigEndian) throws Exception {
17 return intToFloat(bytesToInt(bytes, j, isBigEndian));
20 public static int bytesToShort(byte[] bytes, int j, boolean isBigEndian) {
21 int n = (isBigEndian ? (bytes[j + 1] & 0xff) | (bytes[j] & 0xff) << 8
22 : (bytes[j++] & 0xff) | (bytes[j++] & 0xff) << 8);
23 return (n > 0x7FFF ? n - 0x10000 : n);
26 public static int bytesToInt(byte[] bytes, int j, boolean isBigEndian) {
27 int n = (isBigEndian ? (bytes[j + 3] & 0xff) | (bytes[j + 2] & 0xff) << 8
28 | (bytes[j + 1] & 0xff) << 16 | (bytes[j] & 0xff) << 24
29 : (bytes[j++] & 0xff) | (bytes[j++] & 0xff) << 8
30 | (bytes[j++] & 0xff) << 16 | (bytes[j++] & 0xff) << 24);
34 * return (n > 0x7FFFFFFF ? n - 0x100000000 : n);
42 public static int intToSignedInt(int n) {
46 * return (n > 0x7FFFFFFF ? n - 0x100000000 : n);
53 public static float intToFloat(int x) throws Exception {
55 * see http://en.wikipedia.org/wiki/Binary32
57 * [sign] [8 bits power] [23 bits fraction]
58 * 0x80000000 0x7F800000 0x7FFFFF
64 * if (x == 0) return 0;
65 * var o = javajs.util.BC;
66 * if (o.fracIEEE == null)
68 * var m = ((x & 0x7F800000) >> 23);
69 * return ((x & 0x80000000) == 0 ? 1 : -1) * o.shiftIEEE((x & 0x7FFFFF) | 0x800000, m - 149);
73 return Float.intBitsToFloat(x);
78 * see http://en.wikipedia.org/wiki/Binary64
80 * not concerning ourselves with very small or very large numbers and getting
81 * this exactly right. Just need a float here.
88 public static float bytesToDoubleToFloat(byte[] bytes, int j, boolean isBigEndian) {
90 // IEEE754: sign (1 bit), exponent (11 bits), fraction (52 bits).
91 // seeeeeee eeeeffff ffffffff ffffffff ffffffff xxxxxxxx xxxxxxxx xxxxxxxx
92 // b1 b2 b3 b4 b5 ---------float ignores----
99 * var o = javajs.util.BC;
100 * var b1, b2, b3, b4, b5;
103 * b1 = bytes[j] & 0xFF;
104 * b2 = bytes[j + 1] & 0xFF;
105 * b3 = bytes[j + 2] & 0xFF;
106 * b4 = bytes[j + 3] & 0xFF;
107 * b5 = bytes[j + 4] & 0xFF;
109 * b1 = bytes[j + 7] & 0xFF;
110 * b2 = bytes[j + 6] & 0xFF;
111 * b3 = bytes[j + 5] & 0xFF;
112 * b4 = bytes[j + 4] & 0xFF;
113 * b5 = bytes[j + 3] & 0xFF;
115 * var s = ((b1 & 0x80) == 0 ? 1 : -1);
116 * var e = (((b1 & 0x7F) << 4) | (b2 >> 4)) - 1026;
117 * b2 = (b2 & 0xF) | 0x10;
118 * return s * (o.shiftIEEE(b2, e) + o.shiftIEEE(b3, e - 8) + o.shiftIEEE(b4, e - 16)
119 * + o.shiftIEEE(b5, e - 24));
125 d = Double.longBitsToDouble((((long) bytes[j]) & 0xff) << 56
126 | (((long) bytes[j + 1]) & 0xff) << 48
127 | (((long) bytes[j + 2]) & 0xff) << 40
128 | (((long) bytes[j + 3]) & 0xff) << 32
129 | (((long) bytes[j + 4]) & 0xff) << 24
130 | (((long) bytes[j + 5]) & 0xff) << 16
131 | (((long) bytes[j + 6]) & 0xff) << 8
132 | (((long) bytes[7]) & 0xff));
134 d = Double.longBitsToDouble((((long) bytes[j + 7]) & 0xff) << 56
135 | (((long) bytes[j + 6]) & 0xff) << 48
136 | (((long) bytes[j + 5]) & 0xff) << 40
137 | (((long) bytes[j + 4]) & 0xff) << 32
138 | (((long) bytes[j + 3]) & 0xff) << 24
139 | (((long) bytes[j + 2]) & 0xff) << 16
140 | (((long) bytes[j + 1]) & 0xff) << 8
141 | (((long) bytes[j]) & 0xff));
148 private static float[] fracIEEE;
150 private static void setFracIEEE() {
151 fracIEEE = new float[270];
152 for (int i = 0; i < 270; i++)
153 fracIEEE[i] = (float) Math.pow(2, i - 141);
154 // System.out.println(fracIEEE[0] + " " + Parser.FLOAT_MIN_SAFE);
155 // System.out.println(fracIEEE[269] + " " + Float.MAX_VALUE);
159 * only concerned about reasonable float values here -- private but not designated; called by JavaScript
165 static double shiftIEEE(double f, int i) {
166 if (f == 0 || i < -140)
169 return Float.MAX_VALUE;
170 return f * fracIEEE[i + 140];
175 // for (int i = -50; i < 50; i++) {
176 // float f = i * (float) (Math.random() * Math.pow(2, Math.random() * 100 - 50));
177 // int x = Float.floatToIntBits(f);
178 // int m = ((x & 0x7F800000) >> 23);
179 // float f1 = (float) (f == 0 ? 0 : ((x & 0x80000000) == 0 ? 1 : -1) * shiftIEEE((x & 0x7FFFFF) | 0x800000, m - 149));
180 // System.out.println(f + " " + f1);
182 // System.out.println("binarydo");