Conversion of byte by INT, Char, long, float, and double in Java

Source: Internet
Author: User
The Int, Char, long, float, and double types of Java are converted to bytes.
16:53:40
Add to favorites I want to contribute
[Font: small and big]

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 );
}
}

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.