com.partnersoft.data
Class ByteLib

java.lang.Object
  extended by com.partnersoft.data.ByteLib

public class ByteLib
extends java.lang.Object

A function library for bit-flipping, endianness conversion, and other routine low-level 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.

Copyright 1999-2006 Partner Software, Inc.

Author:
Paul Reavis

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 java.lang.String byteCountToString(double bytes)
          Converts bytecount to a more human-readable String.
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 int[] bytesToInts(byte[] bytes)
          Converts a byte array to a big-endian int array.
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[] intsToBytes(int[] ints)
          Converts a big-endian int array to a byte array (with the same byte sequence).
static java.lang.String intToBinary(int aint)
          Converts a primitive, big-endian int to its binary representation.
static void intToBytes(int value, byte[] bytes, int start)
          Converts an int into a 4-byte array.
static java.lang.String intToHex(int aint)
          Converts a primitive, big-endian int to its hexadecimal representation.
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 java.lang.String longToBinary(long along)
          Converts a primitive, big-endian long to its binary representation.
static void longToBytes(long value, byte[] bytes, int start)
          Converts a lont into an 8-byte array.
static java.lang.String longToHex(long along)
          Converts a primitive, big-endian long to its hexadecimal representation.
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 java.lang.String shortToBinary(short ashort)
          Converts a primitive, big-endian short to its binary representation.
static java.lang.String shortToHex(short ashort)
          Converts a primitive, big-endian short to its hexadecimal representation.
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
 

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.


intsToBytes

public static byte[] intsToBytes(int[] ints)
Converts a big-endian int array to a byte array (with the same byte sequence).

Parameters:
ints - array of input integers
Returns:
byte array with the same byte sequence as the ints

bytesToInts

public static int[] bytesToInts(byte[] bytes)
Converts a byte array to a big-endian int array.

Parameters:
bytes - input byte array
Returns:
int array of big-endian ints converted from the input

byteCountToString

public static java.lang.String byteCountToString(double bytes)
Converts bytecount to a more human-readable String.


longToHex

public static java.lang.String longToHex(long along)
Converts a primitive, big-endian long to its hexadecimal representation.

Parameters:
along - big-endian long
Returns:
hexadecimal string representing the input's bytes

intToHex

public static java.lang.String intToHex(int aint)
Converts a primitive, big-endian int to its hexadecimal representation.

Parameters:
aint - big-endian int
Returns:
hexadecimal string representing the input's bytes

shortToHex

public static java.lang.String shortToHex(short ashort)
Converts a primitive, big-endian short to its hexadecimal representation.

Parameters:
ashort - big-endian short
Returns:
hexadecimal string representing the input's bytes

longToBinary

public static java.lang.String longToBinary(long along)
Converts a primitive, big-endian long to its binary representation.

Parameters:
along - big-endian long
Returns:
binary string representing the input's bytes

intToBinary

public static java.lang.String intToBinary(int aint)
Converts a primitive, big-endian int to its binary representation.

Parameters:
aint - big-endian int
Returns:
binary string representing the input's bytes

shortToBinary

public static java.lang.String shortToBinary(short ashort)
Converts a primitive, big-endian short to its binary representation.

Parameters:
ashort - big-endian short
Returns:
binary string representing the input's bytes