转换字节数组中的字节到整数位阵列和存储 [英] Convert the bytes of byte array to bits and store in Integer array

查看:173
本文介绍了转换字节数组中的字节到整数位阵列和存储的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个字节数组。我需要每个字节的位值存储在一个整数数组。

例如,

字节数组是

 字节HexToBin [] = {(字节)为0x9A(字节)为0xFF,(字节)0×05,(字节)0x16};

那么整数数组应该有

  A = [10011010111111110000010100010110]

我曾尝试以下code,在那里我能够打印每个字节(S2)的二进制值,但我couldnot在整数数组存储ALLBITS

 字节hexToBin [] = {(字节)为0x9A(字节)为0xFF,(字节)0×05,(字节)0x16};
INT [] = ALLBITS新INT [32];
INT A = 0;的for(int i = 0; I< hexToBin.length;我++)
{
  字节eachByte = hexToBin [I]
  字符串s2 =的String.format(%8S,Integer.toBinaryString((eachByte)及为0xFF))。REPLACE('','0');
  的System.out.println(S2);
  的char [] = totalCharArr s2.toCharArray();
  对于(INT K = 0; K< 8; k ++)
  {
      ALLBITS [K +一] = totalCharArr [K];
  }
  A = A + 8;
}对于(INT B = 0; B< 32; b ++)
{
  System.out.print(ALLBITS并[b]);
}

以上code的输出

  10011010
11111111
00000101
00010110
4948484949484948494949494949494948484848484948494848484948494948

整数数组不具备二进制值。

////////////////////////////////////////////////////////////////////////////////////////////////////

感谢您的帮助。

校正code是

 字节hexToBin [] = {(字节)为0x9A(字节)为0xBF,(字节)0×05,(字节)0x16};
INT [] = ALLBITS新INT [32]; //没有位被许可code确定 对于(INT N = 0; N< hexToBin.length; N ++)
  {
    //使用整数,以避免任何可能的混乱,由于符号字节值
    INT sourceByte = 0xFF的及(INT)hexToBin [N]; //转换字节无符号整型
    INT面膜= 0x80的;
    的for(int i = 0; I< 8;我++)
    {
      INT maskResult = sourceByte&安培;面具; //提取单个位
      如果(maskResult大于0){
           ALLBITS [8 * N + I] = 1;
      }
      其他{
           ALLBITS [8 * N + I] = 0; //因为数组不必要的启动零,但良好的文档
      }
      面膜面膜=>> 1;
    }
  }
对于(INT K = 0; K< 32; k ++)
{
  System.out.print(ALLBITS [K]);
}


解决方案

假定为一个循环内 N = 0至3

  //使用整数,以避免任何可能的混乱,由于符号字节值
INT sourceByte = 0xFF的&安培; (中间体)(hexToBin [N]); //将字节无符号整型
INT面膜= 0x80的;
的for(int i = 0; I< 8;我++){
    INT maskResult = sourceByte&安培;面具; //提取单个位
    如果(maskResult!= 0){
         ALLBITS [8 * N + I] = 1;
    }
    其他{
         ALLBITS [8 * N + 1] = 0; //因为数组不必要的inited为零,但良好的机制的文档
    }
    面膜面膜=>> 1;
}

I have bytes in a byte array. I need to store the bit value of each byte in an integer array .

For example ,

the byte array is

byte HexToBin[] = {(byte)0x9A, (byte)0xFF,(byte) 0x05,(byte) 0x16};

then the integer array should have

a = [10011010111111110000010100010110]

I have tried the following code, where i was able to print the binary value of each byte (s2) but i couldnot store in integer array allBits.

byte hexToBin[] = {(byte)0x9A, (byte)0xFF,(byte) 0x05,(byte) 0x16};
int[] allBits = new int[32];
int a =0;

for (int i =0; i < hexToBin.length ; i++)
{
  byte eachByte = hexToBin[i];
  String s2 = String.format("%8s", Integer.toBinaryString((eachByte)& 0xFF)).replace(' ', '0');
  System.out.println(s2);
  char [] totalCharArr = s2.toCharArray();
  for (int k=0; k <8; k++)
  {
      allBits[k+a]= totalCharArr[k];
  }
  a= a+8;
}

for (int b=0; b<32;b++)
{
  System.out.print(allBits[b]);
}

The output of above code is

10011010
11111111
00000101
00010110
4948484949484948494949494949494948484848484948494848484948494948

The integer array does not have the binary value.

////////////////////////////////////////////////////////////////////////////////////////////////////

Thank you for the help

The Corrected code is

byte hexToBin[] = {(byte)0x9A, (byte)0xBF,(byte) 0x05,(byte) 0x16};
int[] allBits = new int[32]; // no of bits is determined by the license code

 for (int n =0; n<hexToBin.length; n++)
  {
    //Use ints to avoid any possible confusion due to signed byte values
    int sourceByte = 0xFF &(int)hexToBin[n];//convert byte to unsigned int
    int mask = 0x80;
    for (int i=0; i<8; i++)
    {
      int maskResult = sourceByte & mask;  // Extract the single bit
      if (maskResult>0) {
           allBits[8*n + i] = 1;
      }
      else {
           allBits[8*n + i] = 0;  // Unnecessary since array is initiated to zero but good documentation
      }
      mask = mask >> 1;
    }
  }


for (int k= 0; k<32; k++)
{
  System.out.print(allBits[k]);
}

解决方案

Assumed to be inside a loop of n = 0 to 3

// Use ints to avoid any possible confusion due to signed byte values
int sourceByte = 0xFF & (int)(hexToBin[n]);  // Convert byte to unsigned int
int mask = 0x80;
for (int i = 0; i < 8; i++) {
    int maskResult = sourceByte & mask;  // Extract the single bit
    if (maskResult != 0) {
         allBits[8*n + i] = 1;
    }
    else {
         allBits[8*n + 1] = 0;  // Unnecessary since array is inited to zero but good documention
    }
    mask = mask >> 1;
}

这篇关于转换字节数组中的字节到整数位阵列和存储的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆