com.jpeterson.util
Class BinaryFormat

java.lang.Object
  |
  +--java.text.Format
        |
        +--com.jpeterson.util.BinaryFormat

public class BinaryFormat
extends java.text.Format

This class allows a number to be easily formatted as a binary number. The representation uses 1's and 0's.

See Also:
Serialized Form

Constructor Summary
BinaryFormat()
          Create a new BinaryFormat object with no divider.
 
Method Summary
 java.lang.String format(byte number)
          Format a byte, returning an 8 bit binary number.
 java.lang.String format(byte[] number)
          Format an array of bytes, returning 8 bits per byte.
 java.lang.StringBuffer format(byte[] number, java.lang.StringBuffer toAppendTo, java.text.FieldPosition pos)
          Format an array of bytes, returning 8 bits per bytes.
 java.lang.StringBuffer format(byte number, java.lang.StringBuffer toAppendTo, java.text.FieldPosition pos)
          Format a byte, returning an 8 bit binary number.
 java.lang.String format(int number)
          Format an int value, returning a 32 bit binary number.
 java.lang.StringBuffer format(int number, java.lang.StringBuffer toAppendTo, java.text.FieldPosition pos)
          Format an int value, returning a 32 bit binary number.
 java.lang.String format(long number)
          Format a long value, returning a 64 bit binary number.
 java.lang.StringBuffer format(long number, java.lang.StringBuffer toAppendTo, java.text.FieldPosition pos)
          Format a long value, returning a 64 bit binary number.
 java.lang.StringBuffer format(java.lang.Object number, java.lang.StringBuffer toAppendTo, java.text.FieldPosition pos)
          Format an object in a binary representation.
 java.lang.String format(short number)
          Format a short value, returning a 16 bit binary number.
 java.lang.StringBuffer format(short number, java.lang.StringBuffer toAppendTo, java.text.FieldPosition pos)
          Format a short value, returning a 16 bit binary number.
 java.lang.String getDivider()
          Get the string used to seperate bits.
static void main(java.lang.String[] args)
           
 java.lang.Number parse(java.lang.String source)
          Parse a binary number into a Number object.
 java.lang.Number parse(java.lang.String text, java.text.ParsePosition parsePosition)
          Parse a binary number into a Number object.
 java.lang.Object parseObject(java.lang.String source, java.text.ParsePosition status)
          Parse a binary number, skipping leading whitespace.
 void setDivider(java.lang.String divider)
          Set the string used to seperate bits.
 
Methods inherited from class java.text.Format
clone, format, parseObject
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

BinaryFormat

public BinaryFormat()
Create a new BinaryFormat object with no divider.
Since:
1.0
Method Detail

format

public java.lang.StringBuffer format(java.lang.Object number,
                                     java.lang.StringBuffer toAppendTo,
                                     java.text.FieldPosition pos)
Format an object in a binary representation. The object number must be an integer Number; Byte, Short, Integer, or Long. If the parameter number is not one of these, this method will throw a IllegalArgumentException.
Overrides:
format in class java.text.Format
Parameters:
number - the number to format
toAppendTo - where the text is to be appended
pos - not used
Returns:
the formatted binary number
Since:
1.0

format

public final java.lang.String format(byte number)
Format a byte, returning an 8 bit binary number.
Parameters:
number - the byte to format
Returns:
the formatted binary number
Since:
1.0

format

public java.lang.StringBuffer format(byte number,
                                     java.lang.StringBuffer toAppendTo,
                                     java.text.FieldPosition pos)
Format a byte, returning an 8 bit binary number.
Parameters:
number - the number to format
toAppendTo - where the text is to be appended
pos - not used
Returns:
the formatted binary number
Since:
1.0

format

public final java.lang.String format(byte[] number)
Format an array of bytes, returning 8 bits per byte. The byte at index zero is the most significant byte, making it possible to enter a stream of bytes received from a serial connection very easily.
Parameters:
number - the bytes to format
Returns:
the formatted binary number
Since:
1.0

format

public java.lang.StringBuffer format(byte[] number,
                                     java.lang.StringBuffer toAppendTo,
                                     java.text.FieldPosition pos)
Format an array of bytes, returning 8 bits per bytes. The byte at index zero is the most significant byte, making it possible to enter a stream of bytes received from a serial connection very easily.
Parameters:
number - the number to format
toAppendTo - where the text is to be appended
pos - not used
Returns:
the formatted binary number
Since:
1.0

format

public java.lang.String format(short number)
Format a short value, returning a 16 bit binary number.
Parameters:
number - the short to format
Returns:
the formatted binary number
Since:
1.0

format

public java.lang.StringBuffer format(short number,
                                     java.lang.StringBuffer toAppendTo,
                                     java.text.FieldPosition pos)
Format a short value, returning a 16 bit binary number.
Parameters:
number - the number to format
toAppendTo - where the text is to be appended
pos - not used
Returns:
the formatted binary number
Since:
1.0

format

public java.lang.String format(int number)
Format an int value, returning a 32 bit binary number.
Parameters:
number - the int to format
Returns:
the formatted binary number
Since:
1.0

format

public java.lang.StringBuffer format(int number,
                                     java.lang.StringBuffer toAppendTo,
                                     java.text.FieldPosition pos)
Format an int value, returning a 32 bit binary number.
Parameters:
number - the number to format
toAppendTo - where the text is to be appended
pos - not used
Returns:
the formatted binary number
Since:
1.0

format

public java.lang.String format(long number)
Format a long value, returning a 64 bit binary number.
Parameters:
number - the long to format
Returns:
the formatted binary number
Since:
1.0

format

public java.lang.StringBuffer format(long number,
                                     java.lang.StringBuffer toAppendTo,
                                     java.text.FieldPosition pos)
Format a long value, returning a 64 bit binary number.
Parameters:
number - the number to format
toAppendTo - where the text is to be appended
pos - not used
Returns:
the formatted binary number
Since:
1.0

parse

public java.lang.Number parse(java.lang.String source)
                       throws java.text.ParseException
Parse a binary number into a Number object. If up to 8 bits are parsed, returns a Byte. If more than 8 and up to 16 bits are parsed, return a Short. If more than 16 and up to 32 bits are parsed, return an Integer. If more than 32 and up to 64 bits are parsed, return a Long.
Parameters:
source - a binary number
Returns:
return an integer form of Number object if parse is successful
Throws:
java.text.ParseException - thrown if source is cannot be converted to a Byte, Short, Int, or Long.
Since:
1.0

parse

public java.lang.Number parse(java.lang.String text,
                              java.text.ParsePosition parsePosition)
Parse a binary number into a Number object. If up to 8 bits are parsed, returns a Byte. If more than 8 and up to 16 bits are parsed, return a Short. If more than 16 and up to 32 bits are parsed, return an Integer. If more than 32 and up to 64 bits are parsed, return a Long.
Parameters:
text - a binary number
parsePosition - position to start parsing from
Returns:
return an integer form of Number object if parse is successful; null otherwise
Since:
1.0

parseObject

public java.lang.Object parseObject(java.lang.String source,
                                    java.text.ParsePosition status)
Parse a binary number, skipping leading whitespace. Does not throw an exception; if no object can be parsed, index is unchanged!
Overrides:
parseObject in class java.text.Format
Parameters:
source - the string to parse
status - the string index to start at
Returns:
The binary number as a Long object.
Since:
1.0

setDivider

public void setDivider(java.lang.String divider)
Set the string used to seperate bits. Is useful some times to insert a space between bits for readability.
Parameters:
divider - String to insert between bits
Since:
1.0
See Also:
getDivider

getDivider

public java.lang.String getDivider()
Get the string used to seperate bits.
Returns:
the string used to seperate bits
Since:
1.0
See Also:
setDivider

main

public static void main(java.lang.String[] args)