// $ Id: BytesHelper. java, v 1.4 2003/06/15 12:45:08 oneovthafew Exp $
Package net. sf. hibernate. util;
/**//**
* This class encapsulates some operations related to Array.
*/
Public final class BytesHelper {
Private BytesHelper (){}
// Convert to int
Public static int toInt (byte [] bytes ){
Int result = 0;
For (int I = 0; I <4; I ++ ){
Result = (result <8)-Byte. MIN_VALUE + (int) bytes [I];
}
Return result;
}
// Convert to short
Public static short toShort (byte [] bytes ){
Return (short) (-(short) Byte. MIN_VALUE + (short) bytes [0]) <8)-(short) Byte. MIN_VALUE + (short) bytes [1]);
}
// Convert int to byte array
Public static byte [] toBytes (int value ){
Byte [] result = new byte [4];
For (int I = 3; I> = 0; I --){
Result [I] = (byte) (0 xFFl & value) + Byte. MIN_VALUE );
Value >>>= 8;
}
Return result;
}
// Convert short to byte array
Public static byte [] toBytes (short value ){
Byte [] result = new byte [2];
For (int I = 1; I> = 0; I --){
Result [I] = (byte) (0 xFFl & value) + Byte. MIN_VALUE );
Value >>>= 8;
}
Return result;
}
// The following is the test code.
Public static void main (String [] args ){
System. out. println (0 + "=" + BytesHelper. toInt (BytesHelper. toBytes (0 )));
System. out. println (1 + "=" + BytesHelper. toInt (BytesHelper. toBytes (1 )));
System. out. println (-1 + "=" + BytesHelper. toInt (BytesHelper. toBytes (-1 )));
System. out. println (Integer. MIN_VALUE + "=" + BytesHelper. toInt (BytesHelper. toBytes (Integer. MIN_VALUE )));
System. out. println (Integer. MAX_VALUE + "=" + BytesHelper. toInt (BytesHelper. toBytes (Integer. MAX_VALUE )));
System. out. println (Integer. MIN_VALUE/2 + "=" + BytesHelper. toInt (BytesHelper. toBytes (Integer. MIN_VALUE/2 )));
System. out. println (Integer. MAX_VALUE/2 + "=" + BytesHelper. toInt (BytesHelper. toBytes (Integer. MAX_VALUE/2 )));
}
}
TIPS:
Why is unit testing unnecessary.