freenet.support
Class HexUtil

java.lang.Object
  extended byfreenet.support.HexUtil

public class HexUtil
extends java.lang.Object

Number in hexadecimal format are used throughout Freenet.

Unless otherwise stated, the conventions follow the rules outlined in the Java Language Specification.

Author:
syoung

Method Summary
static byte[] bitsToBytes(java.util.BitSet ba, int size)
          Pack the bits in ba into a byte[].
static java.lang.String bitsToHexString(java.util.BitSet ba, int size)
          Pack the bits in ba into a byte[] then convert that to a hex string and return it.
static void bytesToBits(byte[] b, java.util.BitSet ba, int maxSize)
          Read bits from a byte array into a bitset
static java.lang.String bytesToHex(byte[] bs)
           
static java.lang.String bytesToHex(byte[] bs, int off, int length)
          Converts a byte array into a string of upper case hex chars.
static void bytesToHexAppend(byte[] bs, int off, int length, java.lang.StringBuffer sb)
           
static int countBytesForBits(int size)
           
static void hexToBits(java.lang.String s, java.util.BitSet ba, int length)
          Read a hex string of bits and write it into a bitset
static byte[] hexToBytes(java.lang.String s)
           
static void hexToBytes(java.lang.String s, byte[] out, int off)
          Converts a String of hex characters into an array of bytes.
static byte[] hexToBytes(java.lang.String s, int off)
           
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

bytesToHex

public static final java.lang.String bytesToHex(byte[] bs,
                                                int off,
                                                int length)
Converts a byte array into a string of upper case hex chars.

Parameters:
bs - A byte array
off - The index of the first byte to read
length - The number of bytes to read.
Returns:
the string of hex chars.

bytesToHexAppend

public static final void bytesToHexAppend(byte[] bs,
                                          int off,
                                          int length,
                                          java.lang.StringBuffer sb)

bytesToHex

public static final java.lang.String bytesToHex(byte[] bs)

hexToBytes

public static final byte[] hexToBytes(java.lang.String s)

hexToBytes

public static final byte[] hexToBytes(java.lang.String s,
                                      int off)

hexToBytes

public static final void hexToBytes(java.lang.String s,
                                    byte[] out,
                                    int off)
                             throws java.lang.NumberFormatException,
                                    java.lang.IndexOutOfBoundsException
Converts a String of hex characters into an array of bytes.

Parameters:
s - A string of hex characters (upper case or lower) of even length.
out - A byte array of length at least s.length()/2 + off
off - The first byte to write of the array
Throws:
java.lang.NumberFormatException
java.lang.IndexOutOfBoundsException

bitsToBytes

public static final byte[] bitsToBytes(java.util.BitSet ba,
                                       int size)
Pack the bits in ba into a byte[].


bitsToHexString

public static final java.lang.String bitsToHexString(java.util.BitSet ba,
                                                     int size)
Pack the bits in ba into a byte[] then convert that to a hex string and return it.


countBytesForBits

public static int countBytesForBits(int size)
Returns:
the number of bytes required to represent the bitset

bytesToBits

public static void bytesToBits(byte[] b,
                               java.util.BitSet ba,
                               int maxSize)
Read bits from a byte array into a bitset

Parameters:
b - the byte[] to read from
ba - the bitset to write to

hexToBits

public static void hexToBits(java.lang.String s,
                             java.util.BitSet ba,
                             int length)
Read a hex string of bits and write it into a bitset

Parameters:
s - hex string of the stored bits
ba - the bitset to store the bits in
length - the maximum number of bits to store