Conversion between Long,int,short and byte arrays in Java

Source: Internet
Author: User

The implementation of the two articles is very comprehensive inside some of the details are different, and now the project schedule is tense. Keep the finishing later.

Article One: http://hi.baidu.com/menglinxi_a/item/35a43d5d50e79212abf6d753


A long type to a byte array
public static byte[] Longtobyte (long number) {
Long temp = number;
Byte[] B = new Byte[8];
for (int i = 0; i < b.length; i++) {
B[i] = new Long (temp & 0xff). Bytevalue ()//
Save the lowest bit in the lowest bit
temp = temp >> 8; Move 8 digits to the right
}
return b;
}

The byte array is converted to long
public static long Bytetolong (byte[] b) {
Long s = 0;
Long S0 = b[0] & 0xff;//Lowest bit
Long S1 = b[1] & 0xff;
Long s2 = b[2] & 0xff;
Long s3 = b[3] & 0xff;
Long S4 = b[4] & 0xff;//Lowest bit
Long S5 = b[5] & 0xff;
Long s6 = b[6] & 0xff;
Long s7 = b[7] & 0xff;

S0, no change.
S1 <<= 8;
S2 <<= 16;
S3 <<= 24;
S4 <<= 8 * 4;
S5 <<= 8 * 5;
S6 <<= 8 * 6;
S7 <<= 8 * 7;
s = S0 | S1 | S2 | S3 | S4 | S5 | S6 | S7;
return s;
}

/**
* Note: the conversion of int to byte array.
*
* @param number
* @return
*/
public static byte[] Inttobyte (int number) {
int temp = number;
Byte[] B = new Byte[4];
for (int i = 0; i < b.length; i++) {
B[i] = new Integer (temp & 0xff). Bytevalue ()//
Save the lowest bit in the lowest bit
temp = temp >> 8; Move 8 digits to the right
}
return b;
}

/**
* Note: Conversion of byte array to int.
*
* @param b
* @return
*/
public static int Bytetoint (byte[] b) {
int s = 0;
int s0 = b[0] & 0xff;//Lowest bit
int S1 = b[1] & 0xff;
int s2 = b[2] & 0xff;
int s3 = b[3] & 0xff;
S3 <<= 24;
S2 <<= 16;
S1 <<= 8;
s = S0 | S1 | S2 | S3;
return s;
}

/**
* Note: The conversion of the short to byte array.
*
* @param s
* @return
*/
public static byte[] Shorttobyte (short number) {
int temp = number;
Byte[] B = new byte[2];
for (int i = 0; i < b.length; i++) {
B[i] = new Integer (temp & 0xff). Bytevalue ()//
Save the lowest bit in the lowest bit
temp = temp >> 8; Move 8 digits to the right
}
return b;
}

/**
* Annotation: byte array to short conversion.
*
* @param b
* @return
*/
public static short Bytetoshort (byte[] b) {
Short s = 0;
Short s0 = (short) (B[0] & 0xff);//Lowest bit
Short S1 = (short) (B[1] & 0xff);
S1 <<= 8;
s = (short) (S0 | s1);
return s;
}

Article Two:

java byte,short, int, long, etc conversion methods

public static byte[] Shorttobytes (short n) {

Byte[] B = new byte[2];

B[1] = (byte) (n & 0xff);

B[0] = (byte) ((n >> 8) & 0xff);

return b;

}

public static short Bytestoshort (byte[] b) {

Return (short) (B[1] & 0xFF

| (B[0] & 0xff) << 8);

}

/**

* int type convert to byte[]

*

* @param num

* Number of INT

* @return byte[]

*/

public static byte[] inttobytes (int num) {

Byte[] B = new Byte[4];

for (int i = 0; i < 4; i++) {

B[i] = (byte) (num >>> (24-I * 8));

}

return b;

}

public static int Bytes2int (byte[] b) {

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.