7-bit解码(java)


上一章中已经详细的说明了7-bit编码的实现,那么要实现解码就只是将编码还原而已。

直接上代码吧:

输入:是一个7-bit编码后的字节数组

输出:解码后的字符串信息


/**
     * 7-bit解码
     * 31D98C06
     * @param data
     * @return
     */
    public static String decode7bit(byte[] d){
        
         byte[] other_mask ={(byte) 0x80,(byte) 0xc0,(byte) 0xe0,(byte) 0xf0,(byte) 0xf8,(byte) 0xfc,(byte) 0xfe};
         byte[] my_mask = {0x7f,0x3f,0x1f,0x0f,0x07,0x03,0x01};
        
         byte other = 0;
         byte temp = 0;
         StringBuffer sb = new StringBuffer();
        
         for(int i=0;i<d.length;i++){
               int index = i%7;
               
               temp = d[i];

               //得到我的数据
               d[i] = (byte) (d[i]&my_mask[index]);
               d[i] = (byte) (d[i] << index);
               
               if(index != 0){
                   d[i] = (byte) (d[i]&other_mask[7-index]);

                   other = (byte) (other>>(8-index));
                   other = (byte) (other&my_mask[7-index]);
                   
                   d[i] = (byte) (d[i] | other);
               }
               
               //先把下一个数据信息拿走
               other = (byte) (temp&other_mask[index]);
               
               sb.append((char)d[i]);
               
               if(index == 6){
                   other = (byte) (other>>1);
                   other = (byte) (other & 0x7f);
                   sb.append((char)other);
               }
         }
         return sb.toString();
    }



转载于:https://my.oschina.net/yjwxh/blog/283597

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值