/**
* Alternate implementation of charToNibble using a precalculated array.
* Based on code by:
* Brian Marquis
* Orion Group Software Engineers http://www.ogse.com
*
* convert a single char to corresponding nibble.
*
* @param c char to convert. must be 0-9 a-f A-F, no
* spaces, plus or minus signs.
*
* @return corresponding integer
* @throws IllegalArgumentException on invalid c.
* @throws ArrayIndexOutOfBoundsException on invalid c
*/
private static int charToNibble ( char c )
{
int nibble = correspondingNibble[c];
if ( nibble < 0 )
{
throw new IllegalArgumentException ( "Invalid hex character: " + c );
}
return nibble;
}
private static byte[] correspondingNibble = new byte['f'+1];
static {
for ( int i=0; i<='f'; i++ )
{
correspondingNibble[i] = -1;
}
for ( int i='0'; i<='9'; i++ )
{
correspondingNibble[i] = (byte)( i - '0' );
}
for ( int i='A'; i<='F'; i++ )
{
correspondingNibble[i] = (byte)( i - 'A' + 10 );
}
for ( int i='a'; i<='f'; i++ )
{
correspondingNibble[i] = (byte)( i - 'a' + 10 );
}
}