关于bit与Byte之间的互相转换

关于bitByte的问题:

一共有4096位,512个字节

0000 0001 ----------> 01

1.      标示“1”的位置,连续用“-”符号

 // 字节串,如01 01 00 01

// 以上字节转换为比特0000 0001 0000 0001 0000 0000 0000 0000 0000  0001                 

// 以上解析出来为8,16,40

// 把字节转化成8个比特后,标识"1"的位置,连续用"-"符号

OctetString lecaoOctetString = new OctetString(value.toString().getBytes(“iso8859_1”));

Byte[] arrays = localOctetString.toByteArray();

List<Integer> resultList = new ArrayList<Integer>();

for(int i=0;i<arrays.length;i++)

{

   Byte[] bits = getBooleanArray(arrays[i]);

   for(int j=0;j<bits.length;j++)

   {

      If(bits[j]==1)

      {

           resultList.add(i*8+j+1);

      }

   }

}

 /**

 * 将字节转换成比特数组

 *

 * @param b

 * @return

 */

 public static byte[] getBooleanArray(byte b)

 {

          byte[] array = new byte[8];

 

          for (int i = 7; i >= 0; i--)

          {

               array[i] = (byte) (b & 1);

               b = (byte) (b >> 1);

          }

        return array;

 }

/**

* 把如"1,3,4,5,7"转化成"1,3-5,7"

*

* @param resultList

* @return

 */

public static String getSimpleString(List<Integer> resultList)

{

        int firstValue = 0;

        int lastValue = 0;

 

       StringBuffer result = new StringBuffer("");

 

       if (resultList == null || resultList.size() < 1)

       {

           return "";

       }

 

      for (int i = 0; i < resultList.size(); i++)

     {

           int currentValue = resultList.get(i);

 

            if (i == 0)

            {

                 firstValue = currentValue;

                 lastValue = currentValue;

 

                if (i == resultList.size() - 1)

               {

                   result.append(currentValue + ",");

               }

                }else

                             {

                                       if (currentValue - lastValue == 1)

                                       {

                                                lastValue = currentValue;

                                                if (i == resultList.size() - 1)

                                                {

                                                         result.append(firstValue + "-" + lastValue + ",");

                                                }

                                       }

                                       else

                                       {

                                                if (firstValue == lastValue)

                                                {

                                                         result.append(firstValue + ",");

                                                }

                                                else if (firstValue < lastValue)

                                                {

                                                         result.append(firstValue + "-" + lastValue + ",");

                                                }

 

                                                firstValue = currentValue;

                                                lastValue = currentValue;

 

                                                if (i == resultList.size() - 1)

                                                {

                                                         result.append(currentValue + ",");

                                                }

                                       }

                             }

                    }

 

                    int lastIndex = result.lastIndexOf(",");

                    if (lastIndex != -1)

                             result.deleteCharAt(lastIndex); //去掉最后一个","

 

                    return result.toString();

           }

 

//例如将13-5,8解析成Byte:

byte[] bytes = new byte[512];

 

byte[] bitMap = new byte[9];

bitMap[0] = 0x00;

bitMap[1] = (byte) 0x80;

bitMap[2] = 0x40;

bitMap[3] = 0x20;

bitMap[4] = 0x10;

bitMap[5] = 0x08;

bitMap[6] = 0x04;

bitMap[7] = 0x02;

bitMap[8] = 0x01;

 

for (Integer num : numbers)

{

    int index = num / 8;

   int subIndex = num % 8;

    bytes[index] |= bitMap[subIndex];

}

 

    String ret = "";

    String charset = "ISO-8859-1";

try

{

    ret = new String(bytes, charset);

} catch (Exception e)

{

     e.printStackTrace();

 }

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值