0

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?

1
  • These are two different questions. You should separate them as such. In other words, "follow-up" questions should be their own question. Commented Mar 30, 2020 at 3:59

2 Answers 2

1

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

1 Comment

close, but do you know how come there would be a ton of 1's printed out, like i was expecting 64 1's or 0's but there is probable twice that
1

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});
  }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.