Import java.io.UnsupportedEncodingException;
public class Contobyte {
/**
* Double Convert byte
* @param arr byte[]
* @param param double Double type parameter
* @param index int
*/
public static void Putdouble (byte[] arr, double param, int index) {
int len = (index-1) + 8;
int arrlen = Arr.length;
Boolean B = Isoutofarrlength (Arrlen, Len); Determines whether the current array length is greater than the converted array length
Long L = double.doubletolongbits (param);
if (b) {
for (int i = 7; I >=0; i--) {
Arr[index + i] = L.bytevalue ();
L = l >> 8;
}
} else {
If the current array length is less than the converted array length, the converted array element is truncated according to index
L = l>> (8*index);
for (int j= arrlen-index-1;j>=0;j--) {
ARR[INDEX+J] = L.bytevalue ();
L = l>>8;
}
}
}
/**
* Float Convert byte
*
* @param arr byte[]
* @param param float Float type parameters
* @param index int
*/
public static void Putfloat (byte[] arr, float param, int index) {
int len = (index-1) + 4;
int arrlen = Arr.length;
Boolean B = Isoutofarrlength (Arrlen, Len); Determines whether the current array length is greater than the converted array length
int L = float.floattointbits (param);
if (b) {
for (int i = 3; I >=0; i--) {
Arr[index + i] = new Integer (l). Bytevalue ();
L = l >> 8;
}
} else {
If the current array length is less than the converted array length, the converted array element is truncated according to index
L = l>> (8*index);
for (int j=arrlen-index-1;j>=0;j--) {
ARR[INDEX+J] = new Integer (l). Bytevalue ();
L =l>>8;
}
}
}
/**
* Character to Byte conversion
*
* @param arr byte[]
* @param ch char char type parameter
* @param index int
* @return
*/
public static void Putchar (byte[] arr, char ch, int index) {
int len = (index-1) + 4;
Boolean B = Isoutofarrlength (arr.length, Len); Determines whether the current array length is greater than the converted array length
if (b) {
int temp = (int) ch;
for (int i = 1; I >=0; i--) {
Arr[index + i] = new Integer (temp & 0xff). Bytevalue ();
temp = temp >> 8;
}
}
}
/**
* Convert long type to byte array
*
* @param arr byte[]
* @param param Long
* @param index int
*/
public static void Putlong (byte[] arr, long param, int index) {
int len = (index-1) + 8;
int arrlen = Arr.length;
Boolean B = Isoutofarrlength (Arrlen, Len); Determines whether the current array length is greater than the converted array length
if (b) {
Arr[index + 0] = (byte) ((param >> & 0xff);
Arr[index + 1] = (byte) ((param >>) & 0xff);
Arr[index + 2] = (byte) ((param >> +) & 0xff);
Arr[index + 3] = (byte) ((param >>) & 0xff);
Arr[index + 4] = (byte) ((param >>) & 0xff);
Arr[index + 5] = (byte) ((param >>) & 0xff);
Arr[index + 6] = (byte) ((param >> 8) & 0xff);
Arr[index + 7] = (byte) (param & 0xff);
} else {
If the current array length is less than the converted array length, the converted array element is truncated according to index
param = param >> (8*index);
for (int i=arrlen-index-1;i>=0;i--) {
Arr[index+i] = (byte) (param & 0xff);
param = param >> 8;
}
}
}
/**
* int type converted to byte array
*
* @param arr byte[]
* @param parameters of type param int int
* @param index int
*/
public static void Putint (byte[] arr, int param, int index) {
int len = (index-1) + 4;
Boolean B = Isoutofarrlength (arr.length, Len); Determines whether the current array length is greater than the converted array length
if (b) {
Arr[index + 0] = (byte) ((param >>) & 0xff);
Arr[index + 1] = (byte) ((param >>) & 0xff);
Arr[index + 2] = (byte) ((param >> 8) & 0xff);
Arr[index + 3] = (byte) (param & 0xff);
}
}
/**
* The short type is converted into a byte array
*
* @param arr byte[]
* @param param Short
* @param index int
*/
public static void Putshort (byte[] arr, short param, int index) {
int len = (index-1) + 2;
Boolean B = Isoutofarrlength (arr.length, Len); Determines whether the current array length is greater than the converted array length
if (b) {
Arr[index + 0] = (byte) ((param >> 8) & 0xff);
Arr[index + 1] = (byte) (param & 0xff);
}
}
/**
* String converted to byte array
*
* @param arr byte[]
* @param str String
* @param index int
* @throws java.io.UnsupportedEncodingException
*/
public static void Putstring (byte[] arr, String str, int index) {
try {
byte[] bb = str.getbytes ("GBK");
int len = index + bb.length;
Boolean B = Isoutofarrlength (arr.length, Len);
if (b) {
for (int i = 0; i < bb.length; i++) {
Arr[index + i] = bb[i];
}
} else {
If the current array length is less than the converted array length, the converted array element is truncated according to index
for (int j=0;j<arr.length-index;j++) {
ARR[INDEX+J] = Bb[j];
}
}
} catch (Unsupportedencodingexception e) {
E.printstacktrace ();
}
}
/**
* Determine if the array subscript is out of bounds
*
* @param arrlength
* Total length of the array
* @param index
* Array Offset
* @return
*/
public static Boolean isoutofarrlength (int arrlength, int index) {
Boolean B;
if (Arrlength > Index) {
B = true;
} else {
b = false;
}
return b;
}
}
Java basic data type converted to byte[] Array