/* 
 * This class provides methods for converting unsigned integers in
 * a byte array into Java primitives. 
 *
 * This library class is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
 *
 * Revision History:
 *
 * Initial version written by Davis Swan in December, 2003.
 */ 
import java.io.*;
import java.sql.*;
import java.util.*;
import java.text.*;
public class UtilUnsigned 
{
/*
 * Use a private constructor so that this class cannot be instantiated.
 */
   private UtilUnsigned() {
   }
/*
 * Convert a byte array to a short.  Short.MIN_VALUE is returned 
 * if the startIndex is greater or equal to the endIndex, or if the
 * resultant unsigned integer is too large to store in a short.
 */
   public static short byteToShort(byte[] inBytes)
   {
      return byteToShort(inBytes,0,inBytes.length);
   }
   public static short byteToShort(byte[] inBytes,int startIndex,int endIndex)
   {
      String hexString;
      short outputShort;
      hexString = byteToHex(inBytes,startIndex,endIndex);
      try
      {
         outputShort = Short.parseShort(hexString,16);
      } catch (Exception ex ) {
         outputShort = Short.MIN_VALUE;
      }
      return outputShort;
   }
/*
 * Convert a byte array to an int.  Integer.MIN_VALUE is returned 
 * if the startIndex is greater or equal to the endIndex, or if the
 * resultant unsigned integer is too large to store in an int.
 */
   public static int byteToInt(byte[] inBytes)
   {
      return byteToInt(inBytes,0,inBytes.length);
   }
   public static int byteToInt(byte[] inBytes,int startIndex,int endIndex)
   {
      String hexString;
      int outputInt;
      hexString = byteToHex(inBytes,startIndex,endIndex);
      try
      {
         outputInt = Integer.parseInt(hexString,16);
      } catch (Exception ex ) {
         outputInt = Integer.MIN_VALUE;
      }
      return outputInt;
   }
/*
 * Convert a byte array to a long.  Long.MIN_VALUE is returned 
 * if the startIndex is greater or equal to the endIndex, or if the
 * resultant unsigned integer is too large to store in a long.
 */
   public static long byteToLong(byte[] inBytes)
   {
      return byteToLong(inBytes,0,inBytes.length);
   }
   public static long byteToLong(byte[] inBytes,int startIndex,int endIndex)
   {
      String hexString;
      long outputLong;
      hexString = byteToHex(inBytes,startIndex,endIndex);
      try
      {
         outputLong = Long.parseLong(hexString,16);
      } catch (Exception ex ) {
         outputLong = Long.MIN_VALUE;
      }
      return outputLong;
   }
/*
 * Convert a byte[] array to Hexadecimal string.
 */
   public static String byteToHex(byte[] inBytes)
   {
      return byteToHex(inBytes,0,inBytes.length);
   }
   public static String byteToHex(byte[] inBytes,int startIndex,int endIndex) 
   {
      byte newByte = 0x00;
      int i,hexIndex; 
      String hexChars = "0123456789ABCDEF";
      StringBuffer outBuffer = new StringBuffer(endIndex - startIndex);
      if ( inBytes == null || endIndex <= startIndex ) return (String)null;
      for ( i = startIndex; i < endIndex; i++) 
      {
/*
 *       Each Hexadecimal character represents 4 bits and each element of
 *       the byte array represents 8 bits.  First strip off the left 4
 *       bits, shift to the least significant (right) portion of a new
 *       byte, then mask the upper portion to allow proper conversion to an 
 *       integer between 0 and 15.  This value can be used as the index into 
 *       the hexadecimal character string.
 */   
         newByte = (byte)(inBytes[i] & 0xF0);  
         newByte = (byte)(newByte >>> 4);     
         newByte = (byte)(newByte & 0x0F); 
         hexIndex = (int)newByte; 
         outBuffer.append(hexChars.substring(hexIndex,hexIndex + 1));
/*
 *       Now strip off the right 4 bits, shift and convert to an integer
 *       between 0 and 15.
 */
         newByte = (byte)(inBytes[i] & 0x0F);
         hexIndex = (int)newByte;
         outBuffer.append(hexChars.substring(hexIndex,hexIndex + 1));
      }
      return outBuffer.toString();
   }
} 


1