"Go" Java byte to Long, double, float, int, short, or long, double, float, int, short to byte

Source: Internet
Author: User

Original URL: http://www.xuebuyuan.com/988752.html

The conversion of Java byte to other data types is mainly used for encoding and decoding binary data, mainly for network transmission, reading and writing binary files, data communication between Java and C + + servers, etc.

The following is a summary of the source code

/**
* Byte to int
*
* @param b
* @return
*/
protected int Bytearraytoint (byte[] b) {
Return (B[0] <<) + ((B[1] & 0xFF) << + ((b[2] & 0xFF) << 8)
+ (B[3] & 0xFF);
}
/**
* Byte turn short
*
* @param b
* @return
*/
protected int Bytearraytoshort (byte[] b) {
Return (B[0] << 8) + (B[1] & 0xFF);
}

/**
* Short to byte data
*
* @param s
* @return
*/
Protected byte[] Shorttobytearray (short s) {
byte[] Shortbuf = new byte[2];
for (int i = 0; i < 2; i++) {
int offset = (Shortbuf.length-1-i) * 8;
Shortbuf[i] = (byte) ((S >>> offset) & 0xff);
}
return shortbuf;
}

/**
* int data to byte data
*
* @param i
* @return
*/
Protected byte[] Inttobytearray (int i) {
Byte[] result = new BYTE[4];
Result[0] = (byte) ((I >>) & 0xFF);
RESULT[1] = (byte) ((I >>) & 0xFF);
RESULT[2] = (byte) ((I >> 8) & 0xFF);
RESULT[3] = (byte) (I & 0xFF);
return result;
}

/**
* Convert long type to byte array
*
* @param bb
* @param x
* @param index
*/
Public byte[] Longtobytearray (long x, int index) {
byte[] bb = new BYTE[8];
Bb[index + 7] = (byte) (x >> 56);
Bb[index + 6] = (byte) (x >> 48);
Bb[index + 5] = (byte) (x >> 40);
Bb[index + 4] = (byte) (x >> 32);
Bb[index + 3] = (byte) (x >> 24);
Bb[index + 2] = (byte) (x >> 16);
Bb[index + 1] = (byte) (x >> 8);
Bb[index + 0] = (byte) (x >> 0);
return BB;
}

/**
* Take a long through a byte array
*
* @param bb
* @param index
* @return
*/
Public long Bytearraytolong (byte[] bb, int index) {
Return ((((long) Bb[index + 7] & 0xff) << 56)
| ((long) Bb[index + 6] & 0xff) << 48)
| ((long) Bb[index + 5] & 0xff) << 40)
| ((long) Bb[index + 4] & 0xff) << 32)
| ((long) Bb[index + 3] & 0xff) << 24)
| ((long) Bb[index + 2] & 0xff) << 16)
| ((long) Bb[index + 1] & 0xff) << 8) | ((long) Bb[index + 0] & 0xff) << 0));
}

/**
* Float Convert byte
*
* @param bb
* @param x
* @param index
*/
public static byte[] Floattobytearray (float v) {
Bytebuffer BB = bytebuffer.allocate (4);
byte[] ret = new BYTE[4];
Floatbuffer fb = Bb.asfloatbuffer ();
Fb.put (v);
Bb.get (ret);
return ret;
}

/**
* Get float via byte array
*
* @param bb
* @param index
* @return
*/
public static float bytearraytofloat (byte[] v) {
Bytebuffer BB = bytebuffer.wrap (v);
Floatbuffer fb = Bb.asfloatbuffer ();
return Fb.get ();
}

/**
* Double Convert byte
*
* @param bb
* @param x
* @param index
*/
Public byte[] Doubletobytearray (double x) {
Bytebuffer BB = bytebuffer.allocate (8);
byte[] ret = new BYTE[8];
DoubleBuffer fb = Bb.asdoublebuffer ();
Fb.put (x);
Bb.get (ret);
return ret;
}

/**
* Get float via byte array
*
* @param bb
* @param index
* @return
*/
Public double bytearraytodouble (byte[] b) {
Bytebuffer BB = bytebuffer.wrap (b);
DoubleBuffer fb = Bb.asdoublebuffer ();
return Fb.get ();
}

Go Java byte to long, double, float, int, short, or long, double, float, int, short to byte

Related Article

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.