com.partnersoft.io
Class BitLib

java.lang.Object
  extended by com.partnersoft.io.BitLib

public class BitLib
extends java.lang.Object

A general library for bit-flipping, endianness conversion, and other routine annoyances.

In general, numbers in weird formats are are supplied as their integer counterparts - for example, the double conversion routines use longs to represent little endian doubles.

Author:
Paul Reavis Copyright 1999 Partner Software, Inc.

Constructor Summary
BitLib()
           
 
Method Summary
static long bigToLittleEndianDouble(double bigInjun)
          Converts a big-endian java double into a little-endian double.
static int bigToLittleEndianFloat(float bigInjun)
          Converts a big-endian java float into a little-endian float.
static int[] blobToInts(byte[] blob)
           
static double bytesToDouble(byte[] bytes)
          Converts an 8-byte array into a double.
static double bytesToDouble(byte[] bytes, int start)
          Converts an 8-byte array section into a double.
static float bytesToFloat(byte[] bytes)
          Converts a 4-byte array into a float.
static float bytesToFloat(byte[] bytes, int start)
          Converts a 4-byte array section into a float.
static java.lang.String bytesToHexString(byte[] buffer)
           
static java.lang.String bytesToHexString(byte[] buffer, int start)
           
static java.lang.String bytesToHexString(byte[] buffer, int start, int length)
           
static int bytesToInt(byte[] bytes)
          Converts a 4-byte array into an int.
static int bytesToInt(byte[] bytes, int start)
          Converts a 4-byte array section into an int.
static long bytesToLong(byte[] bytes)
          Converts an 8-byte array into a long.
static long bytesToLong(byte[] bytes, int start)
          Converts an 8-byte array section into a long.
static java.lang.String bytesToString(byte[] buffer, int start, int width)
           
static int bytesToUnsignedShort(byte[] bytes, int start)
          Converts a 2-byte array section into a positive int.
static int findSequence(byte[] bytes, byte[] sequence, int startIndex)
          Search for given byte array sequence in the given byte array, starting at the given index.
static byte[] hexStringToBytes(java.lang.String hex)
           
static byte[] intsToBlob(int[] ints)
           
static void intToBytes(int value, byte[] bytes, int start)
          Converts an int into a 4-byte array.
static double littleToBigEndianDouble(long littleInjun)
          Converts a little-indian double into a big-endian java double.
static float littleToBigEndianFloat(int littleInjun)
          Converts a little-indian float into a big-endian java float.
static void longToBytes(long value, byte[] bytes, int start)
          Converts a lont into an 8-byte array.
static int reverseByteOrder(int victim)
          Converts a little-endian int to a big endian int and vice-versa.
static long reverseByteOrder(long victim)
          Converts a little-endian long to a big endian long and vice-versa.
static short reverseByteOrder(short victim)
          Converts a little-endian short to a big endian short and vice-versa.
static long reverseByteOrderByWord(long victim)
          Converts a little-endian long to a big endian long, respecting word order, and vice-versa.
static double reverseBytesToDouble(byte[] bytes)
          Converts an 8-byte array into a double reversing as it goes (little-endian).
static double reverseBytesToDouble(byte[] bytes, int start)
          Converts an 8-byte array section into a double reversing as it goes (little-endian).
static float reverseBytesToFloat(byte[] bytes, int start)
          Converts a 4-byte array section into a float reversing as it goes (little-endian).
static int reverseBytesToInt(byte[] bytes)
          Converts a 4-byte array into an int reversing as it goes (little-endian).
static int reverseBytesToInt(byte[] bytes, int start)
          Converts a 4-byte array section into an int reversing as it goes (little-endian).
static long reverseBytesToLong(byte[] bytes)
          Converts an 8-byte array into a long reversing as it goes (little-endian).
static long reverseBytesToLong(byte[] bytes, int start)
          Converts an 8-byte array section into a long reversing as it goes (little-endian).
static int reverseBytesToUnsignedShort(byte[] bytes, int start)
          Converts a 2-byte array section into an reversed short.
static void stringToBytes(java.lang.String value, byte[] bytes, int start)
          Converts a string into a null-terminated ASCII array section.
static void unsignedShortToBytes(int value, byte[] bytes, int start)
          Converts an int into a 2-byte array.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

BitLib

public BitLib()
Method Detail

reverseByteOrder

public static int reverseByteOrder(int victim)
Converts a little-endian int to a big endian int and vice-versa.


reverseByteOrder

public static short reverseByteOrder(short victim)
Converts a little-endian short to a big endian short and vice-versa.


reverseByteOrder

public static long reverseByteOrder(long victim)
Converts a little-endian long to a big endian long and vice-versa.


reverseByteOrderByWord

public static long reverseByteOrderByWord(long victim)
Converts a little-endian long to a big endian long, respecting word order, and vice-versa.


littleToBigEndianDouble

public static double littleToBigEndianDouble(long littleInjun)
Converts a little-indian double into a big-endian java double. The little-endian has to be represented as a long.


bigToLittleEndianDouble

public static long bigToLittleEndianDouble(double bigInjun)
Converts a big-endian java double into a little-endian double. The little-endian has to be represented as a long.


littleToBigEndianFloat

public static float littleToBigEndianFloat(int littleInjun)
Converts a little-indian float into a big-endian java float. The little-endian has to be represented as a int.


bigToLittleEndianFloat

public static int bigToLittleEndianFloat(float bigInjun)
Converts a big-endian java float into a little-endian float. The little-endian has to be represented as a int.


bytesToLong

public static long bytesToLong(byte[] bytes)
Converts an 8-byte array into a long.


bytesToLong

public static long bytesToLong(byte[] bytes,
                               int start)
Converts an 8-byte array section into a long.


reverseBytesToLong

public static long reverseBytesToLong(byte[] bytes)
Converts an 8-byte array into a long reversing as it goes (little-endian).


reverseBytesToLong

public static long reverseBytesToLong(byte[] bytes,
                                      int start)
Converts an 8-byte array section into a long reversing as it goes (little-endian).


bytesToInt

public static int bytesToInt(byte[] bytes)
Converts a 4-byte array into an int.


bytesToInt

public static int bytesToInt(byte[] bytes,
                             int start)
Converts a 4-byte array section into an int.


reverseBytesToUnsignedShort

public static int reverseBytesToUnsignedShort(byte[] bytes,
                                              int start)
Converts a 2-byte array section into an reversed short.


bytesToUnsignedShort

public static int bytesToUnsignedShort(byte[] bytes,
                                       int start)
Converts a 2-byte array section into a positive int.


reverseBytesToInt

public static int reverseBytesToInt(byte[] bytes)
Converts a 4-byte array into an int reversing as it goes (little-endian).


reverseBytesToInt

public static int reverseBytesToInt(byte[] bytes,
                                    int start)
Converts a 4-byte array section into an int reversing as it goes (little-endian).


bytesToDouble

public static double bytesToDouble(byte[] bytes)
Converts an 8-byte array into a double.


bytesToDouble

public static double bytesToDouble(byte[] bytes,
                                   int start)
Converts an 8-byte array section into a double.


bytesToFloat

public static float bytesToFloat(byte[] bytes)
Converts a 4-byte array into a float.


bytesToFloat

public static float bytesToFloat(byte[] bytes,
                                 int start)
Converts a 4-byte array section into a float.


reverseBytesToDouble

public static double reverseBytesToDouble(byte[] bytes)
Converts an 8-byte array into a double reversing as it goes (little-endian).


reverseBytesToDouble

public static double reverseBytesToDouble(byte[] bytes,
                                          int start)
Converts an 8-byte array section into a double reversing as it goes (little-endian).


reverseBytesToFloat

public static float reverseBytesToFloat(byte[] bytes,
                                        int start)
Converts a 4-byte array section into a float reversing as it goes (little-endian).


bytesToString

public static java.lang.String bytesToString(byte[] buffer,
                                             int start,
                                             int width)

hexStringToBytes

public static byte[] hexStringToBytes(java.lang.String hex)

bytesToHexString

public static java.lang.String bytesToHexString(byte[] buffer)

bytesToHexString

public static java.lang.String bytesToHexString(byte[] buffer,
                                                int start)

bytesToHexString

public static java.lang.String bytesToHexString(byte[] buffer,
                                                int start,
                                                int length)

intToBytes

public static void intToBytes(int value,
                              byte[] bytes,
                              int start)
Converts an int into a 4-byte array.


longToBytes

public static void longToBytes(long value,
                               byte[] bytes,
                               int start)
Converts a lont into an 8-byte array.


unsignedShortToBytes

public static void unsignedShortToBytes(int value,
                                        byte[] bytes,
                                        int start)
Converts an int into a 2-byte array.


stringToBytes

public static void stringToBytes(java.lang.String value,
                                 byte[] bytes,
                                 int start)
Converts a string into a null-terminated ASCII array section.


findSequence

public static int findSequence(byte[] bytes,
                               byte[] sequence,
                               int startIndex)
Search for given byte array sequence in the given byte array, starting at the given index. Returns the next index or -1 if not found.


intsToBlob

public static byte[] intsToBlob(int[] ints)

blobToInts

public static int[] blobToInts(byte[] blob)