首页 > 其他 > 详细

boolean与byte的互转

时间:2014-08-16 23:43:41      阅读:470      评论:0      收藏:0      [点我收藏+]
public static byte[] toBytes(boolean[] arrs){
        int f = 0;byte b = 0;
        byte[] ret = new byte[arrs.length/7 + (arrs.length%7==0?0:1)];
        for(int i=0; i<arrs.length; i+=8){
            for(int j=0; j<8; j++){
                if(i+j<arrs.length && arrs[i+j]){
                    b+= (1<<(7-j));
                }
                if(j==7){
                    ret[f++] = b;
                    b = 0;
                }
            }
        }
        return ret;
    }
    
    public static boolean[] toBoolean(byte[] bs, int length){
        boolean[] arrs = new boolean[8*bs.length];
        for(int i=0; i<bs.length; i++){
            int indx = i*8;
            byte b = bs[i];
            for(int j=7; j>=0; j--){
                arrs[indx + j] = (b&1)==1;
                b = (byte)(b>>1);
            }
        }
        return Arrays.copyOfRange(arrs, 0, length);
    }

 

boolean与byte的互转,布布扣,bubuko.com

boolean与byte的互转

原文:http://www.cnblogs.com/inseptember/p/3917027.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!