java中字节与位的转换

本文介绍了在Java中如何进行byte与bit之间的转换,特别适用于解析包含位标志的自定义协议,其中每个位可能代表特定的状态。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

java中byte与bit之间的转换

使用场景:用于解析自定义协议,比如某一位代表一个状态

public class ByteUtils {

    /**
     * 获取一个字节的bit数组
     *
     * @param value
     * @return
     */
    public static byte[] getByteArray(byte value) {
        byte[] byteArr = new byte[8]; //一个字节八位
        for (int i = 7; i > 0; i--) {
            byteArr[i] = (byte) (value & 1); //获取最低位
            value = (byte) (value >> 1); //每次右移一位
        }
        return byteArr;
    }

    /**
     * 把byte转为字符串的bit
     *
     * @param b
     * @return
     */
    public static String byteToBitString(byte b) {
        return ""
                + (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);
    }

    /**
     * 获取一个字节第n位,
     * 思路:右移n位,与1
     *
     * @param value
     * @param index
     * @return
     */
    public static int get(byte value, int index) {
        return (value >> index) & 0x1;
    }

    /**
     * 获取一个字节的第m到第n位
     *
     * @param value
     * @param start >0
     * @param end   >0
     * @return
     */
    public static byte[] getBitRange(byte value, int start, int end) {
        byte[] rangeArray = new byte[end - start + 1];
        if (start > 7 || start < 0) {
            throw new RuntimeException("illegal start param");
        }
        if (end > 7 || end < 0) {
            throw new RuntimeException("illegal end param");
        }
        if (start > end) {
            throw new RuntimeException("start can not bigger than end");
        }
        if (start == end) {
            rangeArray[0] = (byte) ByteUtils.get(value, start);
            return rangeArray;
        }
        for (int i = end; i < start; i--) {
            rangeArray[i] = (byte) ByteUtils.get(value, start);
        }
        return rangeArray;
    }

    /**
     * 位字符串转字节
     * @param str
     * @return
     */
    public static byte bitStringToByte(String str) {
        if(null == str){
            throw new RuntimeException("when bit string convert to byte, Object can not be null!");
        }
        if (8 != str.length()){
            throw new RuntimeException("bit string'length must be 8");
        }
        try{
            //判断最高位,决定正负
            if(str.charAt(0) == '0'){
                return (byte) Integer.parseInt(str,2);
            }else if(str.charAt(0) == '1'){
                return (byte) (Integer.parseInt(str,2) - 256);
            }
        }catch (NumberFormatException e){
            throw new RuntimeException("bit string convert to byte failed, byte String must only include 0 and 1!");
        }

        return 0;
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值