Two types of code that converts int type data to byte arrays and reverse Conversion
Source: Internet
Author: User
/**
* Convert basic data types to byte arrays and reverse conversion methods
* Only the conversion operation is involved, and the parameter is not verified.
* Applicability: RMS operations and Network Data Transmission
*/
Public class dataconvert {
/**
* Convert int type data to byte array
* @ Param n int data
* @ Return: byte array generated
*/
Public static byte [] inttobytes (int n ){
String S = string. valueof (N );
Return S. getbytes ();
}
/**
* Convert byte array to int data
* @ Param B byte array
* @ Return: int data generated
*/
Public static int bytestoint (byte [] B ){
String S = new string (B );
Return integer. parseint (s );
}
/**
* Convert int type data to byte array
* Principle: extract four bytes from the int data and store them separately.
* @ Param n int data
* @ Return: byte array generated
*/
Public static byte [] inttobytes2 (int n ){
Byte [] B = new byte [4];
For (INT I = 0; I <4; I ++ ){
B [I] = (byte) (N> (24-I * 8 ));
}
Return B;
}
/**
* Convert byte array to int data
* @ Param B byte array
* @ Return: int data generated
*/
Public static int bytetoint2 (byte [] B ){
Return (INT) B [0]) <24) + (INT) B [1]) <16) + (INT) B [2]) <8) + B [3];
}
}
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.