ByteUtil工具类大全

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;
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值