118

For example, the bits in a byte B are 10000010, how can I assign the bits to the string str literally, that is, str = "10000010".

Edit

I read the byte from a binary file, and stored in the byte array B. I use System.out.println(Integer.toBinaryString(B[i])). the problem is

(a) when the bits begin with (leftmost) 1, the output is not correct because it converts B[i] to a negative int value.

(b) if the bits begin with 0, the output ignore 0, for example, assume B[0] has 00000001, the output is 1 instead of 00000001

Community
  • 1
  • 1
Sean
  • 4,267
  • 10
  • 36
  • 50

14 Answers14

209

Use Integer#toBinaryString():

byte b1 = (byte) 129;
String s1 = String.format("%8s", Integer.toBinaryString(b1 & 0xFF)).replace(' ', '0');
System.out.println(s1); // 10000001

byte b2 = (byte) 2;
String s2 = String.format("%8s", Integer.toBinaryString(b2 & 0xFF)).replace(' ', '0');
System.out.println(s2); // 00000010

DEMO.

João Silva
  • 89,303
  • 29
  • 152
  • 158
  • I tried this method. In my case, I read the byte from a binary file, and stored in the byte array `B`. I use `System.out.println(Integer.toBinaryString(B[i]))`. When I use this methods, the problem is (a) when the bits begins with (leftmost) 1, the output is not correct because it converts `B[i]` to a negative int value. (b) if the bits begins with 0, the output ignore `0`, for example, assume `B[0]` has `00000001`, the output is `1` instead of `00000001` – Sean Sep 07 '12 at 00:11
  • 3
    @Sean: a) happens because a `byte` in Java is an 8-bit signed two's complement integer. Its minimum value is -128 (2^8), and its maximum value is `127`; b) You can easily fix that by using this `String.format("%8s", Integer.toBinaryString(b)).replace(' ', '0')` to left pad the resulting string with zeros. – João Silva Sep 07 '12 at 00:18
  • 1
    @ João: thanks for your advice. Do you have any idea about how to address (a), how to store the original bit format (begins with 1) into the string? – Sean Sep 07 '12 at 00:25
  • 1
    @Sean: Yes, just `&` it with `0xFF`. – João Silva Sep 07 '12 at 00:29
  • 20
    @Sean: `& 0xFF` basically converts a `signed byte` to an `unsigned integer`. For example, `-129`, like you said, is represented by `11111111111111111111111110000001`. In this case, you basically want the first (least significant) 8 bits, so you AND (`&`) it with `0xFF` (`00000000000000000000000011111111`), effectively cleaning the 1's to the left that we don't care about, leaving out just `10000001`. – João Silva Sep 07 '12 at 01:09
  • @Sean: Ah yes, you need to `replace` the result, *after* the `String.format`, like in the example above. Basically, `String.format` left pads the string with spaces up to 8 `%8s`, and then `replace` takes care of replacing those spaces with `0`s. – João Silva Sep 07 '12 at 01:14
  • I think this example could be improved, because `byte b1 = (byte) 129;` overflows. – Mirco Widmer Feb 08 '14 at 12:24
47

I used this. Similar idea to other answers, but didn't see the exact approach anywhere :)

System.out.println(Integer.toBinaryString((b & 0xFF) + 0x100).substring(1));

0xFF is 255, or 11111111 (max value for an unsigned byte). 0x100 is 256, or 100000000

The & upcasts the byte to an integer. At that point, it can be anything from 0-255 (00000000 to 11111111, I excluded the leading 24 bits). + 0x100 and .substring(1) ensure there will be leading zeroes.

I timed it compared to João Silva's answer, and this is over 10 times faster. http://ideone.com/22DDK1 I didn't include Pshemo's answer as it doesn't pad properly.

Community
  • 1
  • 1
Raekye
  • 5,081
  • 8
  • 49
  • 74
  • hey! got a question about this. I have a Base64 representation string of a PDF, I need to convert into Binary. Basically, Base64->byte->binary.Will this code work? – Sid Dec 12 '16 at 10:32
  • What exactly does the + 0x100 do? You're adding 256 to the resulting integer, but why? – Conner Dassen Mar 27 '19 at 10:18
  • 2
    @ConnerDassen It ensures that the binary string is 0 padded. For example, if `b` is `1`, without `+ 0x100` you will just get `"1"` as your string. By adding `1`, you get `100000001`, and if you take the substring ignoring the first character you will get the proper `"00000001"`. If you do not want your string to be padded you can simply use `Integer.toBinaryString(b & 0xff)`. The `& 0xff` fixes the negative/two's complement issues – Raekye Mar 27 '19 at 21:58
12

Is this what you are looking for?

converting from String to byte

byte b = (byte)(int)Integer.valueOf("10000010", 2);
System.out.println(b);// output -> -126

converting from byte to String

System.out.println(Integer.toBinaryString((b+256)%256));// output -> "10000010"

Or as João Silva said in his comment to add leading 0 we can format string to length 8 and replace resulting leading spaces with zero, so in case of string like " 1010" we will get "00001010"

System.out.println(String.format("%8s", Integer.toBinaryString((b + 256) % 256))
                         .replace(' ', '0'));
Pshemo
  • 122,468
  • 25
  • 185
  • 269
7

You could check each bit on the byte then append either 0 or 1 to a string. Here is a little helper method I wrote for testing:

public static String byteToString(byte b) {
    byte[] masks = { -128, 64, 32, 16, 8, 4, 2, 1 };
    StringBuilder builder = new StringBuilder();
    for (byte m : masks) {
        if ((b & m) == m) {
            builder.append('1');
        } else {
            builder.append('0');
        }
    }
    return builder.toString();
}
Martyn
  • 71
  • 1
  • 1
7

Get each bit of byte and convert to string. Say byte has 8 bits, and we can get them one by one via bit move. For example, we move the second bit of the byte 6 bits to right, the second bit at last of bit of 8 bits, then and(&) with 0x0001 to clean the front bits.

public static String getByteBinaryString(byte b) {
    StringBuilder sb = new StringBuilder();
    for (int i = 7; i >= 0; --i) {
        sb.append(b >>> i & 1);
    }
    return sb.toString();
}
jamee
  • 553
  • 1
  • 5
  • 25
  • Could you please [edit] your answer to give an explanation of why this code answers the question? Code-only answers are [discouraged](http://meta.stackexchange.com/questions/148272), because they don't teach the solution. – DavidPostill Mar 21 '15 at 06:28
  • I needed a little-endian solution, and this one was easiest to adapt (by changing the iteration order to 0 -> 7). Thanks! – gmk57 Mar 09 '21 at 10:07
4

You can work with BigInteger like below example, most especially if you have 256 bit or longer:

String string = "10000010";
BigInteger biStr = new BigInteger(string, 2);

System.out.println("binary: " + biStr.toString(2));
System.out.println("hex: " + biStr.toString(16));
System.out.println("dec: " + biStr.toString(10));

Another example which accepts bytes:

String string = "The girl on the red dress.";

byte[] byteString = string.getBytes(Charset.forName("UTF-8"));
System.out.println("[Input String]: " + string);
System.out.println("[Encoded String UTF-8]: " + byteString);

BigInteger biStr = new BigInteger(byteString);
System.out.println("binary: " + biStr.toString(2)); // binary
System.out.println("hex: " + biStr.toString(16));   // hex or base 16
System.out.println("dec: " + biStr.toString(10));  // this is base 10

Result:

[Input String]: The girl on the red dress.
[Encoded String UTF-8]: [B@70dea4e

binary: 101010001101000011001010010000001100111011010010111001001101100001000000110111101101110001000000111010001101000011001010010000001110010011001010110010000100000011001000111001001100101011100110111001100101110
hex: 546865206769726c206f6e20746865207265642064726573732e

You can also work to convert Binary to Byte format

try {
   System.out.println("binary to byte: " + biStr.toString(2).getBytes("UTF-8"));
} catch (UnsupportedEncodingException e) {e.printStackTrace();}

Note: For string formatting for your Binary format you can use below sample

String.format("%256s", biStr.toString(2).replace(' ', '0'));  // this is for the 256 bit formatting
2

This code will demonstrate how a java int can be split up into its 4 consecutive bytes. We can then inspect each byte using Java methods compared to low level byte / bit interrogation.

This is the expected output when you run the code below:

[Input] Integer value: 8549658

Integer.toBinaryString: 100000100111010100011010
Integer.toHexString: 82751a
Integer.bitCount: 10

Byte 4th Hex Str: 0
Byte 3rd Hex Str: 820000
Byte 2nd Hex Str: 7500
Byte 1st Hex Str: 1a

(1st + 2nd + 3rd + 4th (int(s)) as Integer.toHexString: 82751a
(1st + 2nd + 3rd + 4th (int(s)) ==  Integer.toHexString): true

Individual bits for each byte in a 4 byte int:
00000000 10000010 01110101 00011010

Here is the code to run:

public class BitsSetCount
{
    public static void main(String[] args) 
    {
        int send = 8549658;

        System.out.println( "[Input] Integer value: " + send + "\n" );
        BitsSetCount.countBits(  send );
    }

    private static void countBits(int i) 
    {
        System.out.println( "Integer.toBinaryString: " + Integer.toBinaryString(i) );
        System.out.println( "Integer.toHexString: " + Integer.toHexString(i) );
        System.out.println( "Integer.bitCount: "+ Integer.bitCount(i) );

        int d = i & 0xff000000;
        int c = i & 0xff0000;
        int b = i & 0xff00;
        int a = i & 0xff;

        System.out.println( "\nByte 4th Hex Str: " + Integer.toHexString(d) );
        System.out.println( "Byte 3rd Hex Str: " + Integer.toHexString(c) );
        System.out.println( "Byte 2nd Hex Str: " + Integer.toHexString(b) );
        System.out.println( "Byte 1st Hex Str: " + Integer.toHexString(a) );

        int all = a+b+c+d;
        System.out.println( "\n(1st + 2nd + 3rd + 4th (int(s)) as Integer.toHexString: " + Integer.toHexString(all) );

        System.out.println("(1st + 2nd + 3rd + 4th (int(s)) ==  Integer.toHexString): " + 
                Integer.toHexString(all).equals(Integer.toHexString(i) ) );

        System.out.println( "\nIndividual bits for each byte in a 4 byte int:");

        /*
         * Because we are sending the MSF bytes to a method
         * which will work on a single byte and print some
         * bits we are generalising the MSF bytes
         * by making them all the same in terms of their position
         * purely for the purpose of printing or analysis
         */
        System.out.print( 
                    getBits( (byte) (d >> 24) ) + " " + 
                    getBits( (byte) (c >> 16) ) + " " + 
                    getBits( (byte) (b >> 8) ) + " " + 
                    getBits( (byte) (a >> 0) ) 
        );


    }

    private static String getBits( byte inByte )
    {
        // Go through each bit with a mask
        StringBuilder builder = new StringBuilder();
        for ( int j = 0; j < 8; j++ )
        {
            // Shift each bit by 1 starting at zero shift
            byte tmp =  (byte) ( inByte >> j );

            // Check byte with mask 00000001 for LSB
            int expect1 = tmp & 0x01; 

            builder.append(expect1);
        }
        return ( builder.reverse().toString() );
    }

}
Naresh Maharaj
  • 109
  • 1
  • 1
2

Sorry i know this is a bit late... But i have a much easier way... To binary string :

//Add 128 to get a value from 0 - 255
String bs = Integer.toBinaryString(data[i]+128);
bs = getCorrectBits(bs, 8);

getCorrectBits method :

private static String getCorrectBits(String bitStr, int max){
    //Create a temp string to add all the zeros
    StringBuilder sb = new StringBuilder();
    for(int i = 0; i < (max - bitStr.length()); i ++){
        sb.append("0");
    }

    return sb.toString()+ bitStr;
}
Jp Silver
  • 120
  • 9
2
Integer.toBinaryString((byteValue & 0xFF) + 256).substring(1)
Timofey Gorshkov
  • 4,987
  • 6
  • 41
  • 66
1
String byteToBinaryString(byte b){
    StringBuilder binaryStringBuilder = new StringBuilder();
    for(int i = 0; i < 8; i++)
        binaryStringBuilder.append(((0x80 >>> i) & b) == 0? '0':'1');
    return binaryStringBuilder.toString();
}
Shahidul
  • 2,997
  • 1
  • 21
  • 20
1

A simple answer could be:

System.out.println(new BigInteger(new byte[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0})); // 0
System.out.println(new BigInteger(new byte[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1})); // 1
System.out.println(new BigInteger(new byte[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0})); // 256
System.out.println(new BigInteger(new byte[]{0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0})); // 65536
System.out.println(new BigInteger(new byte[]{0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0})); // 16777216
System.out.println(new BigInteger(new byte[]{0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0})); // 4294967296
System.out.println(new BigInteger(new byte[]{0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0})); // 1099511627776
System.out.println(new BigInteger(new byte[]{0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0})); // 281474976710656
System.out.println(new BigInteger(new byte[]{0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0})); // 72057594037927936
System.out.println(new BigInteger(new byte[]{0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0})); // 18446744073709551616
System.out.println(new BigInteger(new byte[]{0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0})); // 4722366482869645213696
System.out.println(new BigInteger(new byte[]{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0})); // 1208925819614629174706176
System.out.println(Long.MAX_VALUE);                                              // 9223372036854775807
Eric Aya
  • 69,473
  • 35
  • 181
  • 253
Daniel De León
  • 13,196
  • 5
  • 87
  • 72
0

We all know that Java does not provide anything like the unsigned keyword. Moreover, a byte primitive according to the Java's spec represents a value between −128 and 127. For instance, if a byte is cast to an int Java will interpret the first bit as the sign and use sign extension.

Then, how to convert a byte greater than 127 to its binary string representation ??

Nothing prevents you from viewing a byte simply as 8-bits and interpret those bits as a value between 0 and 255. Also, you need to keep in mind that there's nothing you can do to force your interpretation upon someone else's method. If a method accepts a byte, then that method accepts a value between −128 and 127 unless explicitly stated otherwise.

So the best way to solve this is convert the byte value to an int value by calling the Byte.toUnsignedInt() method or casting it as a int primitive (int) signedByte & 0xFF. Here you have an example:

public class BinaryOperations
{
    public static void main(String[] args)
    {
        byte forbiddenZeroBit = (byte) 0x80;

        buffer[0] = (byte) (forbiddenZeroBit & 0xFF);
        buffer[1] = (byte) ((forbiddenZeroBit | (49 << 1)) & 0xFF);
        buffer[2] = (byte) 96;
        buffer[3] = (byte) 234;

        System.out.println("8-bit header:");
        printBynary(buffer);
    }

    public static void printBuffer(byte[] buffer)
    {
        for (byte num : buffer) {
            printBynary(num);
        }
    }

    public static void printBynary(byte num)
    {
        int aux = Byte.toUnsignedInt(num);
        // int aux = (int) num & 0xFF; 
        String binary = String.format("%8s', Integer.toBinaryString(aux)).replace(' ', '0');
        System.out.println(binary);
    }
}

Output

8-bit header:
10000000
11100010
01100000
11101010
Teocci
  • 7,189
  • 1
  • 50
  • 48
0

Just another hint for those who need to massively convert bytes to binary strings: Use a lookup table instead of using those String operation all the time. This is a lot faster than calling the convert function over and over again

public class ByteConverterUtil {

  private static final String[] LOOKUP_TABLE = IntStream.range(0, Byte.MAX_VALUE - Byte.MIN_VALUE + 1)
                                                        .mapToObj(intValue -> Integer.toBinaryString(intValue + 0x100).substring(1))
                                                        .toArray(String[]::new);

  public static String convertByte(final byte byteValue) {
    return LOOKUP_TABLE[Byte.toUnsignedInt(byteValue)];
  }

  public static void main(String[] args){
    System.out.println(convertByte((byte)0)); //00000000
    System.out.println(convertByte((byte)2)); //00000010
    System.out.println(convertByte((byte)129)); //10000001
    System.out.println(convertByte((byte)255)); //11111111
  }


}
rloeffel
  • 144
  • 7
-1

Just guessing here, but if you have a Byte then couldn't you simply invoke toString() on the object to get the value? Or, glancing at the api, using byteValue()?

geogaddi
  • 565
  • 1
  • 8
  • 18