The int, char, long, float, and double types of java are converted to bytes.

Source: Internet
Author: User

Java code
Package com. util;
 
/**
*
* <Ul>
* <Li> file name: com. born. util. ByteUtil. java </li>
* <Li> file Description: byte Conversion Tool </li>
* <Li> copyright: Copyright (C) 2001-2006 </li>
* <Li> company: bran </li>
* <Li> Content Abstract: </li>
* <Li> other instructions: </li>
* <Li> completion date: 2011-7-18 </li>
* <Li> modify record 0: None </li>
* </Ul>
*
* @ Version 1.0
* @ Author Xu liduo
*/
Public class ByteUtil {
/**
* Convert short to byte.
*
* @ Param B
* @ Param s
* Short to be converted
* @ Param index
*/
Public static void putShort (byte B [], short s, int index ){
B [index + 1] = (byte) (s> 8 );
B [index + 0] = (byte) (s> 0 );
}
 
/**
* Get short from byte array
*
* @ Param B
* @ Param index
* Number of digits to start
* @ Return
*/
Public static short getShort (byte [] B, int index ){
Return (short) (B [index + 1] <8) | B [index + 0] & 0xff ));
}
 
/**
* Convert int to byte array
*
* @ Param bb
* @ Param x
* @ Param index
*/
Public static void putInt (byte [] bb, int x, int index ){
Bb [index + 3] = (byte) (x> 24 );
Bb [index + 2] = (byte) (x> 16 );
Bb [index + 1] = (byte) (x> 8 );
Bb [index + 0] = (byte) (x> 0 );
}
 
/**
* Get int through byte array
*
* @ Param bb
* @ Param index
* Start number
* @ Return
*/
Public static int getInt (byte [] bb, int index ){
Return (int) (bb [index + 3] & 0xff) <24)
| (Bb [index + 2] & 0xff) <16)
| (Bb [index + 1] & 0xff) <8) | (bb [index + 0] & 0xff) <0 )));
}
 
/**
* Convert long type to byte array
*
* @ Param bb
* @ Param x
* @ Param index
*/
Public static void putLong (byte [] bb, long x, int index ){
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 );
}
 
/**
* Get long from byte array
*
* @ Param bb
* @ Param index
* @ Return
*/
Public static long getLong (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 ));
}
 
/**
* Character to byte Conversion
*
* @ Param ch
* @ Return
*/
Public static void putChar (byte [] bb, char ch, int index ){
Int temp = (int) ch;
// Byte [] B = new byte [2];
For (int I = 0; I <2; I ++ ){
Bb [index + I] = new Integer (temp & 0xff). byteValue (); // Save the highest bit in the lowest Bit
Temp = temp> 8; // shift 8 digits to the right
}
}
 
/**
* Byte to character conversion
*
* @ Param B
* @ Return
*/
Public static char getChar (byte [] B, int index ){
Int s = 0;
If (B [index + 1]> 0)
S + = B [index + 1];
Else
S + = 256 + B [index + 0];
S * = 256;
If (B [index + 0]> 0)
S + = B [index + 1];
Else
S + = 256 + B [index + 0];
Char ch = (char) s;
Return ch;
}
 
/**
* Float conversion byte
*
* @ Param bb
* @ Param x
* @ Param index
*/
Public static void putFloat (byte [] bb, float x, int index ){
// Byte [] B = new byte [4];
Int l = Float. floatToIntBits (x );
For (int I = 0; I <4; I ++ ){
Bb [index + I] = new Integer (l). byteValue ();
L = l> 8;
}
}
 
/**
* Float obtained through byte array
*
* @ Param bb
* @ Param index
* @ Return
*/
Public static float getFloat (byte [] B, int index ){
Int l;
L = B [index + 0];
L & = 0xff;
L | = (long) B [index + 1] <8 );
L & = 0 xffff;
L | = (long) B [index + 2] <16 );
L & = 0 xffffff;
L | = (long) B [index + 3] <24 );
Return Float. intBitsToFloat (l );
}
 
/**
* Double conversion byte
*
* @ Param bb
* @ Param x
* @ Param index
*/
Public static void putDouble (byte [] bb, double x, int index ){
// Byte [] B = new byte [8];
Long l = Double. doubleToLongBits (x );
For (int I = 0; I <4; I ++ ){
Bb [index + I] = new Long (l). byteValue ();
L = l> 8;
}
}
 
/**
* Float obtained through byte array
*
* @ Param bb
* @ Param index
* @ Return
*/
Public static double getDouble (byte [] B, int index ){
Long l;
L = B [0];
L & = 0xff;
L | = (long) B [1] <8 );
L & = 0 xffff;
L | = (long) B [2] <16 );
L & = 0 xffffff;
L | = (long) B [3] <24 );
L & = 0 xffffffffl;
L | = (long) B [4] <32 );
L & = 0 xffffffffl;
L | = (long) B [5] <40 );
L & = 0 xffffffffffl;
L | = (long) B [6] <48 );
L & = 0 xffffffffffffl;
L | = (long) B [7] <56 );
Return Double. longBitsToDouble (l );
}
}

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.