From 71ae6cd4f933ed57d7a26457e8ab31a6fc16a025 Mon Sep 17 00:00:00 2001 From: Robert Greene Date: Wed, 26 Feb 2003 01:29:12 +0000 Subject: [PATCH] Added getSaneNumber and getSignedWordValue. --- .../applecommander/storage/AppleUtil.java | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/src/com/webcodepro/applecommander/storage/AppleUtil.java b/src/com/webcodepro/applecommander/storage/AppleUtil.java index 9f164c7..4fa60cd 100644 --- a/src/com/webcodepro/applecommander/storage/AppleUtil.java +++ b/src/com/webcodepro/applecommander/storage/AppleUtil.java @@ -62,6 +62,14 @@ public class AppleUtil { return getUnsignedByte(low) + getUnsignedByte(high)*256; } + /** + * Compute the signed value of a word. + */ + public static int getSignedWordValue(byte[] buffer, int offset) { + int value = buffer[offset+1] * 256; + return value + getUnsignedByte(buffer[offset]); + } + /** * Set a word value. */ @@ -404,4 +412,35 @@ public class AppleUtil { } return decompressedStream.toByteArray(); } + + /** + * Pull a SANE formatted number from the buffer and return it + * as a Java double datatype. Fortunately, SANE is the IEEE 754 + * format which _is_ Java's double datatype. The Double class + * has an intrinsic longBitsToDouble method to do this. The + * SANE/IEEE 754 format is setup as such: + *
+	 *   E SSSSSSSSSSS FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF...F
+ * 0 1........11 12....................................63
+ *
+ * Where E is the sign bit, S is the exponent bits and F is the + * fraction bits. The format is discussed within the Double class + * documentation as around the web. Be aware that the fraction + * bits are base 2. Meaning that a fraction of .101 is, in reality, + * a binary fraction. In decimal, this is 1/2 + 0/4 + 1/8 = 5/8 + * or .625. + * See http://www.psc.edu/general/software/packages/ieee/ieee.html + * for an example. + *

+ * Note: SANE numbers, as stored by AppleWorks are in typical + * low/high format. + */ + public static double getSaneNumber(byte[] buffer, int offset) { + long doubleBits = 0; + for (int i=8; i>0; i--) { + doubleBits <<= 8; + doubleBits+= getUnsignedByte(buffer[offset+i-1]); + } + return Double.longBitsToDouble(doubleBits); + } }