標籤:
import java.io.UnsupportedEncodingException;
public class ConToByte {
/**
* double轉換byte
* @param arr byte[]
* @param param double double類型的參數
* @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); //判斷當前數組長度是否大於轉換的數組長度
Long l = Double.doubleToLongBits(param);
if (b) {
for (int i = 7; i >=0; i--) {
arr[index + i] = l.byteValue();
l = l >> 8;
}
} else {
// 如果當前數組長度小於轉換的數組長度,就根據index截取轉換的數組元素
l = l>>(8*index);
for(int j= arrLen-index-1;j>=0;j--){
arr[index+j] = l.byteValue();
l = l>>8;
}
}
}
/**
* float轉換byte
*
* @param arr byte[]
* @param param float float類型的參數
* @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); //判斷當前數組長度是否大於轉換的數組長度
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 {
// 如果當前數組長度小於轉換的數組長度,就根據index截取轉換的數組元素
l = l>>(8*index);
for(int j=arrLen-index-1;j>=0;j--){
arr[index+j] = new Integer(l).byteValue();
l =l>>8;
}
}
}
/**
* 字元到位元組轉換
*
* @param arr byte[]
* @param ch char char類型的參數
* @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); //判斷當前數組長度是否大於轉換的數組長度
if (b) {
int temp = (int) ch;
for (int i = 1; i >=0; i--) {
arr[index + i] = new Integer(temp & 0xff).byteValue();
temp = temp >> 8;
}
}
}
/**
* 轉換long型為byte數組
*
* @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); //判斷當前數組長度是否大於轉換的數組長度
if (b) {
arr[index + 0] = (byte) ((param >> 56) & 0xff);
arr[index + 1] = (byte) ((param >> 48) & 0xff);
arr[index + 2] = (byte) ((param >> 40) & 0xff);
arr[index + 3] = (byte) ((param >> 32) & 0xff);
arr[index + 4] = (byte) ((param >> 24) & 0xff);
arr[index + 5] = (byte) ((param >> 16) & 0xff);
arr[index + 6] = (byte) ((param >> 8) & 0xff);
arr[index + 7] = (byte) (param & 0xff);
} else {
// 如果當前數組長度小於轉換的數組長度,就根據index截取轉換的數組元素
param = param >> (8*index);
for(int i=arrLen-index-1;i>=0;i--){
arr[index+i] = (byte) (param & 0xff);
param = param >> 8;
}
}
}
/**
* int類型轉換成byte數組
*
* @param arr byte[]
* @param 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); //判斷當前數組長度是否大於轉換的數組長度
if (b) {
arr[index + 0] = (byte) ((param >> 24) & 0xff);
arr[index + 1] = (byte) ((param >> 16) & 0xff);
arr[index + 2] = (byte) ((param >> 8) & 0xff);
arr[index + 3] = (byte) (param & 0xff);
}
}
/**
* short類型轉換成byte數組
*
* @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); //判斷當前數組長度是否大於轉換的數組長度
if (b) {
arr[index + 0] = (byte) ((param >> 8) & 0xff);
arr[index + 1] = (byte) (param & 0xff);
}
}
/**
* 字串轉換成byte數組
*
* @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 {
// 如果當前數組長度小於轉換的數組長度,就根據index截取轉換的數組元素
for(int j=0;j<arr.length-index;j++){
arr[index+j] = bb[j];
}
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
/**
* 判斷數組下標是否越界
*
* @param arrLength
* 數組總長度
* @param index
* 數組位移量
* @return
*/
public static boolean isOutOfArrLength(int arrLength, int index) {
boolean b;
if (arrLength > index) {
b = true;
} else {
b = false;
}
return b;
}
}
java基礎資料型別 (Elementary Data Type)轉換成byte[]數組