Java 将字节数组中的字节转换为位并存储在整数数组中

Java 将字节数组中的字节转换为位并存储在整数数组中,java,arrays,string,byte,bits,Java,Arrays,String,Byte,Bits,我有字节数组中的字节。我需要将每个字节的位值存储在整数数组中 比如说, 字节数组是 byte HexToBin[] = {(byte)0x9A, (byte)0xFF,(byte) 0x05,(byte) 0x16}; 那么整数数组应该有 a = [10011010111111110000010100010110] 我尝试了下面的代码,我可以打印每个字节(s2)的二进制值,但我不能将所有位存储在整数数组中 byte hexToBin[] = {(byte)0x9A, (byte)0xFF,(

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

比如说,

字节数组是

byte HexToBin[] = {(byte)0x9A, (byte)0xFF,(byte) 0x05,(byte) 0x16};
那么整数数组应该有

a = [10011010111111110000010100010110]
我尝试了下面的代码,我可以打印每个字节(s2)的二进制值,但我不能将所有位存储在整数数组中

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]);
}
整数数组没有二进制值

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

谢谢你的帮助

更正后的代码为

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]);
}
byte hexToBin[]={(字节)0x9A,(字节)0xBF,(字节)0x05,(字节)0x16};
int[]allBits=new int[32];//位的数量由许可证代码决定
对于(int n=0;n>1;
}
}

对于(int k=0;k请尝试
System.out.print((char)allBits[b]);
或尝试将所有位声明为char[],而不是int[]。

假定在
n
=0到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;
}
//使用ints可避免由于有符号字节值而引起的任何可能的混淆
int sourceByte=0xFF&(int)(hexToBin[n]);//将字节转换为无符号int
int mask=0x80;
对于(int i=0;i<8;i++){
int maskResult=sourceByte&mask;//提取单个位
if(maskResult!=0){
所有位[8*n+i]=1;
}
否则{
allBits[8*n+1]=0;//不需要,因为数组初始化为零,但文档化良好
}
掩码=掩码>>1;
}

首先,不要以大写字母开头变量名。标准Java和C约定(Microsoft之外)以小写字母开始变量和方法名称。我已经更新了codeNext,使用toBinaryString还有很长的路要走。你有一个
字节
。如果你将字节中的0x80位设置为0x80,那么结果将非零。你可以坐在一个循环中,将“掩码”向右移动(使用
)从0x80到0x40到0x20..,测试每一位,并相应地将相应的数组项设置为1或0。(请记住,“0”的数值为48,“1”的数值为49。)@HotLicks。你能给我一个如何用0x80屏蔽循环中字节的每一位的示例吗。谢谢你的代码。是而不是if(maskResult),应该有if(maskResult>0)?在allBits[8*n+1]=0;中,它应该是allBits[8*n+i]=0;。我将在上面的部分发布更正后的代码,您能检查一下吗?。它工作正常..谢谢..@user3048644-我忘了Java需要一个比较--我主要使用C,不需要比较。更新为比较等于零。(比较>0也可以。)@用户3048644——当然,如果你打算把它交上来,你最好能够解释它是如何工作的。
// 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;
}