package com.byte;
import java.nio.charset.Charset;
public class ByteUtil {
* 将byte字节数组写入到buffer字节数据的i位置
*/
public static int writeByte(byte[] buffer, byte from, int i) {
buffer[i++] = from;
return i;
}
/**
* 将int字节数组写入到buffer字节数据的i位置
*/
public static int writeInt(byte[] buffer, byte[] from, int i) {
buffer[i++] = from[0];
buffer[i++] = from[1];
buffer[i++] = from[2];
buffer[i++] = from[3];
return i;
}
/**
* 将short字节数组写入到buffer字节数据的i位置
*/
public static int writeShort(byte[] buffer, byte[] from, int i) {
buffer[i++] = from[0];
buffer[i++] = from[1];
return i;
}
/**
* 将double字节数组写入到buffer字节数据的i位置
*/
public static int writeDouble(byte[] buffer, byte[] from, int i) {
buffer[i++] = from[0];
buffer[i++] = from[1];
buffer[i++] = from[2];
buffer[i++] = from[3];
buffer[i++] = from[4];
buffer[i++] = from[5];
buffer[i++] = from[6];
buffer[i++] = from[7];
return i;
}
/**
* 将long字节数组写入到buffer字节数据的i位置
*/
public static int writeLong(byte[] buffer, byte[] from, int i) {
buffer[i++] = from[0];
buffer[i++] = from[1];
buffer[i++] = from[2];
buffer[i++] = from[3];
buffer[i++] = from[4];
buffer[i++] = from[5];
buffer[i++] = from[6];
buffer[i++] = from[7];
return i;
}
/**
* 将String字节数组写入到buffer字节数据的i位置
*/
public static int writeString(byte[] buffer, byte[] from, int i) {
for(int j = 0; j < from.length; j++) {
buffer[i++] = from[j];
}
return i;
}
/**
* 将int类型的数据转换成byte数组
*/
public static byte[] getBytes(int data) {
byte[] bytes = new byte[4];
bytes[3] = (byte) (data & 0xff);
bytes[2] = (byte) ((data & 0xff00) >> 8);
bytes[1] = (byte) ((data & 0xff0000) >> 16);
bytes[0] = (byte) ((data & 0xff000000) >> 24);
return bytes;
}
/**
* 将short类型的数据转换成byte数组
*/
public static byte[] getBytes(short data) {
byte[] bytes = new byte[2];
bytes[1] = (byte) (data & 0xff);
bytes[0] = (byte) ((data & 0xff00) >> 8);
return bytes;
}
/**
* 将double类型的数据转换成byte数组
*/
public static byte[] getBytes(double data) {
long longBits = Double.doubleToLongBits(data);
return getBytes(longBits);
}
/**
* 将long类型的数据转换成byte数组
*/
public static byte[] getBytes(long data) {
byte[] bytes = new byte[8];
bytes[7] = (byte) (data & 0xff);
bytes[6] = (byte) ((data >> 8) & 0xff);
bytes[5] = (byte) ((data >> 16) & 0xff);
bytes[4] = (byte) ((data >> 24) & 0xff);
bytes[3] = (byte) ((data >> 32) & 0xff);
bytes[2] = (byte) ((data >> 40) & 0xff);
bytes[1] = (byte) ((data >> 48) & 0xff);
bytes[0] = (byte) ((data >> 56) & 0xff);
return bytes;
}
/**
* 将String类型的数据转换成byte数组
* 默认UTF-8编码
*/
public static byte[] getBytes(String data) {
return getBytes(data, Charset.forName("UTF-8"));
}
/**
* 将String类型的数据转换成byte数组
*/
public static byte[] getBytes(String data, String charset) {
return getBytes(data, Charset.forName(charset));
}
/**
* 将String类型的数据转换成byte数组
*/
public static byte[] getBytes(String data, Charset charset) {
return data.getBytes(charset);
}
/**
* 将byte数组转成int类型的数据
*/
public static int getInt(byte[] bytes) {
return ((bytes[0] & 0xFF) << 24) + ((bytes[1] & 0xFF) << 16) + ((bytes[2] & 0xFF) << 8) + (bytes[3] & 0xFF);
}
/**
* 将byte数组转成short类型的数据
*/
public static short getShort(byte[] bytes) {
return (short) (((bytes[0] & 0xFF) << 8) + (bytes[1] & 0xFF));
}
/**
* 将byte数组转成long类型的数据
*/
public static long getLong(byte[] bytes) {
return (0xffL & (long)bytes[7]) |
(0xff00L & ((long)bytes[6] << 8)) |
(0xff0000L & ((long)bytes[5] << 16)) |
(0xff000000L & ((long)bytes[4] << 24)) |
(0xff00000000L & ((long)bytes[3] << 32)) |
(0xff0000000000L & ((long)bytes[2] << 40)) |
(0xff000000000000L & ((long)bytes[1] << 48)) |
(0xff00000000000000L & ((long)bytes[0] << 56));
}
/**
* 将byte数组转成double类型的数据
*/
public static double getDouble(byte[] bytes) {
long l = getLong(bytes);
return Double.longBitsToDouble(l);
}
/**
* 将byte数组转成String类型的数据
* 默认UTF-8编码
*/
public static String getString(byte[] bytes) {
return new String(bytes, Charset.forName("utf-8"));
}
/**
* 将byte数组转成String类型的数据
*/
public static String getString(byte[] bytes, String charset) {
return new String(bytes, Charset.forName(charset));
}
/**
* 将byte数组转成String类型的数据
*/
public static String getString(byte[] bytes, Charset charset) {
return new String(bytes, charset);
}
/**
* 截取byte数组中的一部分
*/
public static byte[] subBytes(byte[] src, int begin, int length) {
byte[] bs = new byte[length];
for (int i = begin; i < begin + length; i++) {
bs[i - begin] = src[i];
}
return bs;
}
}
ByteUtil工具类大全
最新推荐文章于 2024-07-19 05:30:00 发布