org.mbari.util
Class NumberUtil

java.lang.Object
  extended by org.mbari.util.NumberUtil

public class NumberUtil
extends java.lang.Object

Useful static methods for manipulating numbers.

Version:
$Id: NumberUtil.java,v 1.1 2008/11/04 22:17:57 bobh Exp $
Author:
MBARI

Method Summary
static byte[] toByteArray(char c)
          Converts an char to the corresponding byte[] array.
static byte[] toByteArray(char c, boolean isReverseOrder)
          Description of the Method
static byte[] toByteArray(double d)
          Converts a double to the corresponding byte[] array.
static byte[] toByteArray(double d, boolean isReverseOrder)
          Converts a double to the corresponding byte[] array
static byte[] toByteArray(float f)
          Converts a float to the corresponding byte[] array.
static byte[] toByteArray(float f, boolean isReverseOrder)
          Converts a float to the corresponding byte[] array
static byte[] toByteArray(int i)
          Converts an integer to the corresponding byte[] array.
static byte[] toByteArray(int i, boolean isReverseOrder)
          Converts an integer to the corresponding byte[] array
static byte[] toByteArray(long l)
          Converts a long to the corresponding byte[] array.
static byte[] toByteArray(long l, boolean isReverseOrder)
          Converts a long to the corresponding byte[] array
static byte[] toByteArray(short i)
          Converts a short to the corresponding byte[] array.
static byte[] toByteArray(short i, boolean isReverseOrder)
          Converts a short to the corresponding byte[] array
static char toChar(byte[] si)
          Convert a byte[] (most significant byte first) to the corresponding char value.
static char toChar(byte[] si, boolean isReverseOrder)
          Convert a byte[] to the corresponding char value.
static double toDouble(byte[] si)
          Convert a byte[] (most significant byte first) to the corresponding double value.
static double toDouble(byte[] si, boolean isReverseOrder)
          Convert a byte[] to the corresponding double value.
static float toFloat(byte[] si)
          Convert a byte[] (most significant byte first) to the corresponding float value.
static float toFloat(byte[] si, boolean isReverseOrder)
          Convert a byte[] to the corresponding float value.
static java.lang.String toHexString(byte[] b)
          Format a byte array as a hexadecimal string.
static int toInt(byte[] si)
          Convert a byte[] (most significant byte first) to the corresponding int value.
static int toInt(byte[] si, boolean isReverseOrder)
          Convert a byte[] to the corresponding int value.
static long toLong(byte[] si)
          Convert a byte[] (most significant byte first) to the corresponding long value.
static long toLong(byte[] si, boolean isReverseOrder)
          Convert a byte[] to the corresponding long value.
static short toShort(byte[] si)
          Convert a byte[] (most significant byte first) to the corresponding short value.
static short toShort(byte[] si, boolean isReverseOrder)
          Convert a byte[] to the corresponding short value.
static int toUnsignedInt(byte b)
          Convert an unsigned byte to an integer
static int toUnsignedInt(byte lsb, byte msb)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

toByteArray

public static final byte[] toByteArray(char c)
Converts an char to the corresponding byte[] array. Most significant byte is first.

Parameters:
c - char input
Returns:
Byte array corresponding to the input

toByteArray

public static final byte[] toByteArray(double d)
Converts a double to the corresponding byte[] array. Most significant byte is first.

Parameters:
d - Integer input
Returns:
Byte array corresponding to the input

toByteArray

public static final byte[] toByteArray(float f)
Converts a float to the corresponding byte[] array. Most significant byte is first.

Parameters:
f - float input
Returns:
Byte array corresponding to the input

toByteArray

public static final byte[] toByteArray(int i)
Converts an integer to the corresponding byte[] array. Most significant byte is first.

Parameters:
i - Integer input
Returns:
Byte array corresponding to the input

toByteArray

public static final byte[] toByteArray(long l)
Converts a long to the corresponding byte[] array. Most significant byte is first.

Parameters:
l - long input
Returns:
Byte array corresponding to the input

toByteArray

public static final byte[] toByteArray(short i)
Converts a short to the corresponding byte[] array. Most significant byte is first.

Parameters:
i - Integer input
Returns:
Byte array corresponding to the input

toByteArray

public static final byte[] toByteArray(char c,
                                       boolean isReverseOrder)
Description of the Method

Parameters:
c - Description of the Parameter
isReverseOrder - Description of the Parameter
Returns:
Description of the Return Value

toByteArray

public static final byte[] toByteArray(double d,
                                       boolean isReverseOrder)
Converts a double to the corresponding byte[] array

Parameters:
isReverseOrder - True if the ordering is least significant byte to most signifigant byte, false for most significant to least.
d - Description of the Parameter
Returns:
Byte array corresponding to the input

toByteArray

public static final byte[] toByteArray(float f,
                                       boolean isReverseOrder)
Converts a float to the corresponding byte[] array

Parameters:
f - Short input
isReverseOrder - True if the ordering is least significant byte to most signifigant byte, false for most significant to least.
Returns:
Byte array corresponding to the input

toByteArray

public static final byte[] toByteArray(int i,
                                       boolean isReverseOrder)
Converts an integer to the corresponding byte[] array

Parameters:
i - Integer input
isReverseOrder - True if the ordering is least significant byte to most signifigant byte, false for most significant to least.
Returns:
Byte array corresponding to the input

toByteArray

public static final byte[] toByteArray(long l,
                                       boolean isReverseOrder)
Converts a long to the corresponding byte[] array

Parameters:
isReverseOrder - True if the ordering is least significant byte to most signifigant byte, false for most significant to least.
l - Description of the Parameter
Returns:
Byte array corresponding to the input

toByteArray

public static final byte[] toByteArray(short i,
                                       boolean isReverseOrder)
Converts a short to the corresponding byte[] array

Parameters:
i - Short input
isReverseOrder - True if the ordering is least significant byte to most signifigant byte, false for most significant to least.
Returns:
Byte array corresponding to the input

toChar

public static final char toChar(byte[] si)
Convert a byte[] (most significant byte first) to the corresponding char value.

Parameters:
si - the input array
Returns:
The value coresponding to the byte array

toChar

public static final char toChar(byte[] si,
                                boolean isReverseOrder)
Convert a byte[] to the corresponding char value.

Parameters:
si - the input array
isReverseOrder - True if little-endian. False if big-endian (Most significant byte first)
Returns:
The value coresponding to the byte array

toDouble

public static final double toDouble(byte[] si)
Convert a byte[] (most significant byte first) to the corresponding double value.

Parameters:
si - the input array
Returns:
The value coresponding to the byte array

toDouble

public static final double toDouble(byte[] si,
                                    boolean isReverseOrder)
Convert a byte[] to the corresponding double value.

Parameters:
si - the input array
isReverseOrder - True if little-endian. False if big-endian (Most significant byte first)
Returns:
The value coresponding to the byte array

toFloat

public static final float toFloat(byte[] si)
Convert a byte[] (most significant byte first) to the corresponding float value.

Parameters:
si - the input array
Returns:
The value coresponding to the byte array

toFloat

public static final float toFloat(byte[] si,
                                  boolean isReverseOrder)
Convert a byte[] to the corresponding float value.

Parameters:
si - the input array
isReverseOrder - True if little-endian. False if big-endian (Most significant byte first)
Returns:
The value coresponding to the byte array

toInt

public static final int toInt(byte[] si)
Convert a byte[] (most significant byte first) to the corresponding int value.

Parameters:
si - the input array
Returns:
The value coresponding to the byte array

toInt

public static final int toInt(byte[] si,
                              boolean isReverseOrder)
Convert a byte[] to the corresponding int value.

Parameters:
si - the input array
isReverseOrder - True if little-endian. False if big-endian (Most significant byte first)
Returns:
The value coresponding to the byte array

toLong

public static final long toLong(byte[] si)
Convert a byte[] (most significant byte first) to the corresponding long value.

Parameters:
si - the input array
Returns:
The value coresponding to the byte array

toLong

public static final long toLong(byte[] si,
                                boolean isReverseOrder)
Convert a byte[] to the corresponding long value.

Parameters:
si - the input array
isReverseOrder - True if little-endian. False if big-endian (Most significant byte first)
Returns:
The value coresponding to the byte array

toShort

public static final short toShort(byte[] si)
Convert a byte[] (most significant byte first) to the corresponding short value.

Parameters:
si - the input array
Returns:
The signed short value coresponding to the byte array

toShort

public static final short toShort(byte[] si,
                                  boolean isReverseOrder)
Convert a byte[] to the corresponding short value.

Parameters:
si - the input array
isReverseOrder - True if little-endian. False if big-endian (Most significant byte first)
Returns:
The signed short value coresponding to the byte array

toUnsignedInt

public static int toUnsignedInt(byte b)
Convert an unsigned byte to an integer

Parameters:
b - byte to convert
Returns:
integer corresponding to the unsigned byte.

toUnsignedInt

public static int toUnsignedInt(byte lsb,
                                byte msb)
Parameters:
lsb - The least significant byte
msb - The most significant byte
Returns:
An unsigned 'short'. In order to get the full range of an unsigned short a Java int is returned.

toHexString

public static java.lang.String toHexString(byte[] b)
Format a byte array as a hexadecimal string. For example
 byte[] bytes = new byte[]{(byte)0x12, (byte)0x0F, (byte)0xF0};
 String hex = NumberUtil.toHexString(bytes); // 120ff0



Copyright © 2003 MBARI.

The Monterey Bay Aquarium Research Institute (MBARI) provides this documentation and code "as is", with no warranty, express or implied, of its quality or consistency. It is provided without support and without obligation on the part of MBARI to assist in its use, correction, modification, or enhancement.