KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jxl > biff > DoubleHelper


1 /*********************************************************************
2 *
3 * Copyright (C) 2001 Andrew Khan
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 ***************************************************************************/

19
20 package jxl.biff;
21
22 /**
23  * Class to help handle doubles
24  */

25 public class DoubleHelper
26 {
27   /**
28    * Private constructor to prevent instantiation
29    */

30   private DoubleHelper()
31   {
32   }
33
34   /**
35    * Gets the IEEE value from the byte array passed in
36    *
37    * @param pos the position in the data block which contains the double value
38    * @param data the data block containing the raw bytes
39    * @return the double value converted from the raw data
40    */

41   public static double getIEEEDouble(byte[] data, int pos)
42   {
43     int num1 = IntegerHelper.getInt(data[pos], data[pos + 1],
44                                     data[pos + 2], data[pos + 3]);
45     int num2 = IntegerHelper.getInt(data[pos + 4], data[pos + 5],
46                                     data[pos + 6], data[pos + 7]);
47
48     // Long.parseLong doesn't like the sign bit, so have to extract this
49
// information and put it in at the end. (Acknowledgment: thanks
50
// to Ruben for pointing this out)
51
boolean negative = ((num2 & 0x80000000) != 0);
52
53     // Thanks to Lyle for the following improved IEEE double processing
54
long val = ((num2 & 0x7fffffff) * 0x100000000L) +
55                   (num1 < 0 ? 0x100000000L + num1 : num1);
56     double value = Double.longBitsToDouble(val);
57
58     if (negative)
59     {
60       value = -value;
61     }
62     return value;
63   }
64
65   /**
66    * Puts the IEEE representation of the double provided into the array
67    * at the designated position
68    *
69    * @param target the data block into which the binary representation is to
70    * be placed
71    * @param pos the position in target in which to place the bytes
72    * @param d the double value to convert to raw bytes
73    */

74   public static void getIEEEBytes(double d, byte[] target, int pos)
75   {
76     long val = Double.doubleToLongBits(d);
77     target[pos] = (byte) (val & 0xff);
78     target[pos + 1] = (byte) ((val & 0xff00) >> 8);
79     target[pos + 2] = (byte) ((val & 0xff0000) >> 16);
80     target[pos + 3] = (byte) ((val & 0xff000000) >> 24);
81     target[pos + 4] = (byte) ((val & 0xff00000000L) >> 32);
82     target[pos + 5] = (byte) ((val & 0xff0000000000L) >> 40);
83     target[pos + 6] = (byte) ((val & 0xff000000000000L) >> 48);
84     target[pos + 7] = (byte) ((val & 0xff00000000000000L) >> 56) ;
85   }
86 }
87
88
89
90
Popular Tags