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);
32 return (/** @j2sNative (n|0) || */ n);
36 // * return (n > 0x7FFFFFFF ? n - 0x100000000 : n);
44 public static int intToSignedInt(int n) {
46 return (/** @j2sNative n || */ n);
51 // * return (n > 0x7FFFFFFF ? n - 0x100000000 : n);
58 public static float intToFloat(int x) throws Exception {
60 // // see http://en.wikipedia.org/wiki/Binary32
62 // // [sign] [8 bits power] [23 bits fraction]
63 // // 0x80000000 0x7F800000 0x7FFFFF
68 // boolean isJS = /** xxxxxxj2sNative true | */false;
72 // if (fracIEEE == null)
74 // int m = ((x & 0x7F800000) >> 23);
75 // * return ((x & 0x80000000) == 0 ? 1 : -1) * o.shiftIEEE$D$I((x & 0x7FFFFF) | 0x800000, m - 149);
82 // * if (x == 0) return 0;
83 // * var o = javajs.util.BC;
84 // * if (o.fracIEEE$ == null)
85 // * o.setFracIEEE$();
86 // * var m = ((x & 0x7F800000) >> 23);
87 // * return ((x & 0x80000000) == 0 ? 1 : -1) * o.shiftIEEE$D$I((x & 0x7FFFFF) | 0x800000, m - 149);
91 return Float.intBitsToFloat(x);
96 * see http://en.wikipedia.org/wiki/Binary64
98 * not concerning ourselves with very small or very large numbers and getting
99 * this exactly right. Just need a float here.
106 public static float bytesToDoubleToFloat(byte[] bytes, int j, boolean isBigEndian) {
108 // IEEE754: sign (1 bit), exponent (11 bits), fraction (52 bits).
109 // seeeeeee eeeeffff ffffffff ffffffff ffffffff xxxxxxxx xxxxxxxx xxxxxxxx
110 // b1 b2 b3 b4 b5 ---------float ignores----
112 if (fracIEEE == null)
117 * var b1, b2, b3, b4, b5;
120 * b1 = bytes[j] & 0xFF;
121 * b2 = bytes[j + 1] & 0xFF;
122 * b3 = bytes[j + 2] & 0xFF;
123 * b4 = bytes[j + 3] & 0xFF;
124 * b5 = bytes[j + 4] & 0xFF;
126 * b1 = bytes[j + 7] & 0xFF;
127 * b2 = bytes[j + 6] & 0xFF;
128 * b3 = bytes[j + 5] & 0xFF;
129 * b4 = bytes[j + 4] & 0xFF;
130 * b5 = bytes[j + 3] & 0xFF;
132 * var s = ((b1 & 0x80) == 0 ? 1 : -1);
133 * var e = (((b1 & 0x7F) << 4) | (b2 >> 4)) - 1026;
134 * b2 = (b2 & 0xF) | 0x10;
135 * return s * (C$.shiftIEEE$D$I(b2, e) +C$.shiftIEEE$D$I(b3, e - 8) + C$.shiftIEEE$D$I(b4, e - 16)
136 * + C$.shiftIEEE$D$I(b5, e - 24));
142 d = Double.longBitsToDouble((((long) bytes[j]) & 0xff) << 56
143 | (((long) bytes[j + 1]) & 0xff) << 48
144 | (((long) bytes[j + 2]) & 0xff) << 40
145 | (((long) bytes[j + 3]) & 0xff) << 32
146 | (((long) bytes[j + 4]) & 0xff) << 24
147 | (((long) bytes[j + 5]) & 0xff) << 16
148 | (((long) bytes[j + 6]) & 0xff) << 8
149 | (((long) bytes[7]) & 0xff));
151 d = Double.longBitsToDouble((((long) bytes[j + 7]) & 0xff) << 56
152 | (((long) bytes[j + 6]) & 0xff) << 48
153 | (((long) bytes[j + 5]) & 0xff) << 40
154 | (((long) bytes[j + 4]) & 0xff) << 32
155 | (((long) bytes[j + 3]) & 0xff) << 24
156 | (((long) bytes[j + 2]) & 0xff) << 16
157 | (((long) bytes[j + 1]) & 0xff) << 8
158 | (((long) bytes[j]) & 0xff));
165 private static float[] fracIEEE;
167 private static void setFracIEEE() {
168 fracIEEE = new float[270];
169 for (int i = 0; i < 270; i++)
170 fracIEEE[i] = (float) Math.pow(2, i - 141);
171 // System.out.println(fracIEEE[0] + " " + Parser.FLOAT_MIN_SAFE);
172 // System.out.println(fracIEEE[269] + " " + Float.MAX_VALUE);
176 * only concerned about reasonable float values here -- private but not designated; called by JavaScript
182 static double shiftIEEE(double f, int i) {
183 if (f == 0 || i < -140)
186 return Float.MAX_VALUE;
187 return f * fracIEEE[i + 140];
192 // for (int i = -50; i < 50; i++) {
193 // float f = i * (float) (Math.random() * Math.pow(2, Math.random() * 100 - 50));
194 // int x = Float.floatToIntBits(f);
195 // int m = ((x & 0x7F800000) >> 23);
196 // float f1 = (float) (f == 0 ? 0 : ((x & 0x80000000) == 0 ? 1 : -1) * shiftIEEE((x & 0x7FFFFF) | 0x800000, m - 149));
197 // System.out.println(f + " " + f1);
199 // System.out.println("binarydo");