java基础之byte转换工具类

1、Byte[] 转 byte[] 

public static byte[] toPrimitives(Byte[] oBytes) {
        byte[] bytes = new byte[oBytes.length];
        for (int i = 0; i < oBytes.length; i++) {
            bytes[i] = oBytes[i];
        }
        return bytes;
}

2、byte[] 转 Byte[]

public static Byte[] toObjects(byte[] bytesPrim) {
    Byte[] bytes = new Byte[bytesPrim.length];
    int i = 0;
    for (byte b : bytesPrim) bytes[i++] = b; // Autoboxing
    return bytes;
} 

3、byte转十六进制字符

public static String byteToHex(byte b) {
    String hex = Integer.toHexString(b & 0xFF);
    if (hex.length() == 1) {
        hex = '0' + hex;
    }
    return hex.toUpperCase(Locale.getDefault());
}

4、byte[] 转 字符串的bit

public static String byteToBit(byte[] bs) {
    String result = "";
    for (byte b : bs) {
        result = result
                + (byte) ((b >> 7) & 0x1) + (byte) ((b >> 6) & 0x1)
                + (byte) ((b >> 5) & 0x1) + (byte) ((b >> 4) & 0x1)
                + (byte) ((b >> 3) & 0x1) + (byte) ((b >> 2) & 0x1)
                + (byte) ((b >> 1) & 0x1) + (byte) ((b >> 0) & 0x1);
    }
    return result;
}

5、String 转 byte[]

public static byte[] stringToByteArr(String value, String decode) {
    try {
        return value.getBytes(decode);
    } catch (UnsupportedEncodingException e) {
        logger.error("String转化为byte数组", e);
    }
    return null;
}

6、byte[] 转 String

public static String byteArrToString(byte[] arr, String decode) {
    try {
        if (arr.length == 0) {
            return null;
        }
        return new String(arr, decode);
    } catch (UnsupportedEncodingException e) {
        logger.error("byte[] 数组转为 String", e);
    }
    return null;
}

7、byte[] 转 数值

/**
 * byte[] 转为一个数值
 *
 * @param arr   byte数组
 * @param isBig 是否大端模式
 * @param trim  字节长度(long=8, int=4, short=2, byte=1)
 * @return
*/
public static Number byteArrToNumber(byte[] arr, boolean isBig, int trim) {
    if (arr == null || arr.length == 0) {
        return null;
    }
    // 舍弃掉数组长度超过8的部分
    if (arr.length > trim) {
        for (int i = trim; i < arr.length; i++) {
            arr[i] = 0;
        }
    }
    long total = 0;
    for (int i = 0; i < arr.length; i++) {
        long arrVal = arr[i] & 0xFF;
        arrVal = isBig ? arrVal << ((arr.length - i - 1) * 8) : arrVal << ((i) * 8);
        total |= arrVal;
    }
    return total;
}

8、数值 转 byte[]

public static byte[] numberToByteArr(Number number, boolean isBig, int trim) {
    if (number == null) {
        return null;
    }
    if (trim <= 0) {
        return null;
    }
    long value = number.longValue();
    byte[] arr = new byte[trim];
    for (int i = 0; i < trim; i++) {
        long val = value >> (i * 8);
        val &= 0xff;
        if (isBig) {
            arr[trim - i - 1] = (byte) val;
        } else {
            arr[i] = (byte) val;
        }
    }
    return arr;
}

9、多个数组合并一个数组

public static byte[] arrayCopy(byte[]...arrays) {
    //数组长度
    int arrayLength = 0;
    //目标数组的起始位置
    int startIndex = 0;
    for (byte[] bytes: arrays) {
        arrayLength = arrayLength + bytes.length;
    }
    byte[] byteArray = new byte[arrayLength];
    for (int i = 0; i < arrays.length; i++) {
        if (i > 0) {
            //i为0时,目标数组的起始位置为0 ,i为1时,目标数组的起始位置为第一个数组长度
            //i为2时,目标数组的起始位置为第一个数组长度+第二个数组长度
            startIndex = startIndex + arrays[i - 1].length;
        }
        System.arraycopy(arrays[i], 0, byteArray, startIndex, arrays[i].length);
    }
    return byteArray;
}
public static byte[] arrayCopy(List<byte[]> list) {
    //数组长度
    int arrayLength = 0;
    //目标数组的起始位置
    int startIndex = 0;
    for (byte[] bytes: list) {
        arrayLength = arrayLength + bytes.length;
    }
    byte[] byteArray = new byte[arrayLength];
    for (int i = 0; i < list.size(); i++) {
        if (i > 0) {
            //i为0时,目标数组的起始位置为0 ,i为1时,目标数组的起始位置为第一个数组长度
            //i为2时,目标数组的起始位置为第一个数组长度+第二个数组长度
            startIndex = startIndex + list.get(i - 1).length;
        }
        System.arraycopy(list.get(i), 0, byteArray, startIndex, list.get(i).length);
    }
    return byteArray;
}

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值