Source code-net. sf. hibernate. util. BytesHelper

Source: Internet
Author: User
// $ 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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.