0

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]);
}
4
  • First off, don't start variable names with a upper-case letter. Standard Java and C convention (outside of Microsoft) to to start variable and method names with a lower-case letter. Commented Jan 15, 2014 at 1:36
  • Next, using toBinaryString is the long way around. You have a byte. If you AND the byte with 0x80 the result will be non-zero if the 0x80 bit in the byte is set. You can sit in a loop, shifting a "mask" to the right (with >>) from 0x80 to 0x40 to 0x20.., testing each bit and setting the corresponding array entry to 1 or 0 accordingly. Commented Jan 15, 2014 at 1:40
  • 1
    (And keep in mind that "0" has the numeric value 48 and "1" has the numeric value 49.) Commented Jan 15, 2014 at 1:42
  • @HotLicks . can you give me an example how to mask the each bit of a byte in loop with 0x80. Commented Jan 15, 2014 at 1:59

2 Answers 2

1

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;
}
Sign up to request clarification or add additional context in comments.

3 Comments

thank you for the code. is instead of if(maskResult), there should be if(maskResult>0) ? in allBits[8*n + 1] = 0; , it should be allBits[8*n + i] = 0;. I will post the corrected code in the above part, can you please check it ?.. it is working fine .. thank you..
@user3048644 - I forgot that Java requires a compare there -- I work mostly with C where the compare isn't required. Updated to compare equal to zero. (Comparing > 0 works OK, too.)
@user3048644 - And, of course, if you're going to turn that in you'd better be able to explain how it works.
1

Try System.out.print((char)allBits[b]); or try declaring allBits as char[], not int[].

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.