java数组转换数据类型_Java中字节数组与基本数据类型的转换 | 学步园

ByteTools工具类,将基本数据类型转换成字节数组

public class ByteTools {

/**

* Convert short to byte array.

* @param number

* @return

*/

public static byte[] shortToByteArray(short number) {

int temp = number;

byte[] b = new byte[2];

for (int i = 0; i < b.length; i++) {

b[i] = Integer.valueOf(temp & 0xff).byteValue();

temp = temp >> 8;

}

return b;

}

/**

* int类型转换成ByteArray

*

* @param integer

* @return

*/

public static byte[] intToByteArray(int integer) {

int byteNum = (40 - Integer.numberOfLeadingZeros(integer < 0 ? ~integer

: integer)) / 8;

byte[] byteArray = new byte[4];

for (int n = 0; n < byteNum; n++)

byteArray[3 - n] = (byte) (integer >>> (n * 8));

return (byteArray);

}

/**

* float类型转换成ByteArray

*

* @param ft

* @return

*/

public static byte[] floatToByteArray(float ft) {

ByteArrayOutputStream boutput = new ByteArrayOutputStream();

DataOutputStream doutput = new DataOutputStream(boutput);

try {

doutput.writeFloat(ft);

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

byte[] buf = boutput.toByteArray();

return buf;

}

/**

* double类型转换成ByteArray

*

* @param val

* @return

*/

public static byte[] doubleToByteArray(double val) {

ByteArrayOutputStream boutput = new ByteArrayOutputStream();

DataOutputStream doutput = new DataOutputStream(boutput);

try {

doutput.writeDouble(val);

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

byte[] buf = boutput.toByteArray();

return buf;

}

/**

* String类型转换成ByteArray

*

* @param str

* @return

*/

public static byte[] stringToByteArray(String str) {

return str.getBytes();

}

/**

* ByteArray类型的累加

*

* @param byte1

* @param byte2

* @return

*/

public static byte[] addByteArray(byte[] byte1, byte[] byte2) {

byte[] data = new byte[byte1.length + byte2.length];

System.arraycopy(byte1, 0, data, 0, byte1.length);

System.arraycopy(byte2, 0, data, byte1.length, byte2.length);

return data;

}

/**

* ByteArray类型的累加

*

* @param byte1

* @param byte2

* @return

*/

public static byte[] addByteArray(byte[] byte1, byte[] byte2, byte[] byte3) {

byte[] data = addByteArray(addByteArray(byte1, byte2), byte3);

return data;

}

/**

* 四个byte累加

* @param byte1

* @param byte2

* @param byte3

* @param byte4

* @return

*/

public static byte[] addByteArray(byte[] byte1, byte[] byte2,byte[] byte3, byte[] byte4) {

byte[] data1 = addByteArray(byte1,byte2);

byte[] data2 = addByteArray(byte3,byte4);

return addByteArray(data1,data2);

}

}

ByteParseTools工具类,将字节数组转换成基本数据类型

public class ByteParseTools {

/**

* 截取byte数据

*

* @param b

* 是byte数组

* @param j

* 是大小

* @param k

* 是从哪里开始

* @return

*/

public static byte[] cutOutByte(byte[] b, int j, int k) {

if (b.length == 0 || j == 0) {

return null;

}

byte[] bjq = new byte[j];

for (int i = 0; i < j; i++) {

bjq[i] = b[i + k];

}

return bjq;

}

/**

* 将字节数组转成int类型

*

* @param b

* @return

*/

public static int byteToInt(byte[] b) {

int mask = 0xff;

int temp = 0;

int n = 0;

for (int i = 0; i < 4; i++) {

n <<= 8;

temp = b[i] & mask;

n |= temp;

}

return n;

}

/**

* Convert byte array to short.

*

* @param b

* @return

*/

public static short byteToShort(byte[] b) {

short s = 0;

short s0 = (short) (b[0] & 0xff);

short s1 = (short) (b[1] & 0xff);

s1 <<= 8;

s = (short) (s0 | s1);

return s;

}

/**

* 将byte转成String

*

* @param data

* @return

*/

public static String byteToString(byte[] data) {

return new String(data);

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值