erp
erp

Reputation: 3034

convert bit array values into a string of binary numbers

Just look at lower part now

I've seen on here before ways to convert hex to binary, but my question is: is it possible to convert hex values inside of a byte array into binary values and put it into a big string?

The code I've been working on so far is:

public static void main (String[] args){

    byte [] ex;
    ex = new byte [] {(byte)0xFF, (byte)0x11, (byte)0xEE, (byte)0x22, (byte)0xDD, (byte)0x33, (byte)0xCC, (byte)0x44};
    printByteArray(ex);

}

public static void printByteArray(byte [] array)


{
    System.out.print("[  ");
    for(int i = 0; i < array.length; i++)
    {
        System.out.print(Integer.toHexString((array[i]>>4)&0x0F).toUpperCase());
        System.out.print(Integer.toHexString(array[i]&0x0F).toUpperCase() + "  ");
    }
    System.out.println( "]");
}

What I want is to change is to be able to put the whole binary string into a byte array, i.e., to get the binary values for each hex number and then put it all into on HUGE byte array.

EDITED

Okay well I'm going with the first one it doesn't matter all that much I guess how long they are, they were still correct. But now could you just help me take and use that string. I have this code :

public static void main (String[] args){

    String binary;
    byte [] ex;
    ex = new byte [] {(byte)0xFF, (byte)0x11, (byte)0xEE, (byte)0x22, (byte)0xDD, (byte)0x33, (byte)0xCC, (byte)0x44};
    printByteArray(ex);
    binary = hexToBin(ex);
    System.out.println(binary);

}

public static String hexToBin(byte [] array)
{
    String binStr = null;
    for(int i = 0; i < array.length; i++){
        binStr.append(Integer.toBinaryString(array[i]));
    }
    return binStr;
}

... but due to the fact of how I initialized the string (at first) my output has null in it. Any ideas how to fix that? Also I just changed it to append and now I don't know why but won't let me use it?

Upvotes: 0

Views: 2637

Answers (2)

Gus
Gus

Reputation: 6881

Try this (note that byte is 8 bit not 16 as you seem to suggest above, which may be part of the confusion, for 16 bits you would want a short and 32 would be int)

public class PrintBytes {

  public static void printByteArray(byte [] array)

  {
    System.out.print("[ ");
    for (byte anArray : array) {
      byte b = anArray;
      for (int j = 0; j < 8; j++) {
        System.out.print((b & 128) < 1 ? "0" : "1");
        b <<= 1;
      }
      System.out.print(" ");
    }
    System.out.println( "]");
  }

  public static void main(String[] args) {
    printByteArray(new byte[] {64, 32, -1});
  }
}

Upvotes: 1

Brandon
Brandon

Reputation: 10058

Would Integer.toBinaryString give you what you want?

http://docs.oracle.com/javase/6/docs/api/java/lang/Integer.html#toBinaryString(int)

public static void printByteArray(byte [] array)
{
    System.out.print("[  ");
    for(int i = 0; i < array.length; i++)
    {
        System.out.print(Integer.toBinaryString(array[i]));
    }
    System.out.println( "]");
}

Upvotes: 1

Related Questions