How to convert a byte to its binary string representation

时间:2023-03-09 16:30:44
How to convert a byte to its binary string representation

How to convert a byte to its binary string representation

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

byte b1 = (byte) ;
String s1 = String.format("%8s", Integer.toBinaryString(b1 & 0xFF)).replace(' ', '');
System.out.println(s1); // byte b2 = (byte) ;
String s2 = String.format("%8s", Integer.toBinaryString(b2 & 0xFF)).replace(' ', '');
System.out.println(s2); //
public class BitsSetCount
{
public static void main(String[] args)
{
int send = ; 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 >> ) ) + " " +
getBits( (byte) (c >> ) ) + " " +
getBits( (byte) (b >> ) ) + " " +
getBits( (byte) (a >> ) )
); } private static String getBits( byte inByte )
{
// Go through each bit with a mask
StringBuilder builder = new StringBuilder();
for ( int j = ; j < ; 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() );
} }
public static String byteToString(byte b) {
byte[] masks = { -, , , , , , , };
StringBuilder builder = new StringBuilder();
for (byte m : masks) {
if ((b & m) == m) {
builder.append('');
} else {
builder.append('');
}
}
return builder.toString();
}
public static String getByteBinaryString(byte b) {
StringBuilder sb = new StringBuilder();
for (int i = ; i >= ; --i) {
sb.append(b >>> i & );
}
return sb.toString();
}
String byteToBinaryString(byte b){
StringBuilder binaryStringBuilder = new StringBuilder();
for(int i = ; i < ; i++)
binaryStringBuilder.append(((0x80 >>> i) & b) == ? '':'');
return binaryStringBuilder.toString();
}