如何将一个字节转换为二进制string表示

例如,一个字节B中的位是10000010 ,我怎样才能将这些位字面地分配给stringstr ,即str = "10000010"

编辑

我从二进制文件中读取字节,并存储在字节数组B 。 我使用System.out.println(Integer.toBinaryString(B[i])) 。 问题是

(a)当位开始于(最左边)1时,输出不正确,因为它将B[i]转换为负的int值。

(b)如果比特以0开始,输出忽略0 ,例如,假设B[0]有00000001,输出是1而不是00000001

使用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

我用这个 类似的想法,以其他答案,但没有看到确切的方法在任何地方:)

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

0xFF是255或11111111 (无符号字节的最大值)。 0x100是256或100000000

&将该字节上传到一个整数。 在这一点上,它可以是02550000000011111111 ,我排除了前24位)。 + 0x100.substring(1)确保将有前导零。

我比较了JoãoSilva的回答 ,这个速度超过了10倍。 http://ideone.com/22DDK1我没有包括Pshemo的答案,因为它没有正确填充。;

这是你想要的?

 //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" 

或者如JoãoSilva在评论中所说

 System.out.println(String.format("%8s", Integer.toBinaryString((b + 256) % 256)).replace(' ', '0')); 

这段代码将演示如何将一个java int分成4个连续的字节。 然后,我们可以使用Java方法比较低字节/位查询来检查每个字节。

当您运行下面的代码时,这是预期的输出:

 [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 

这里是运行的代码:

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

您可以检查字节上的每个位,然后将0或1附加到一个string。 这里是我为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(); } 

对不起,我知道这有点晚了…但我有一个更简单的方法…二进制string:

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

getCorrectBits方法:

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

只是猜测在这里,但如果你有一个字节,那么你不能简单地调用对象的toString()来获得值? 或者,看着api ,使用byteValue()?

获取字节的每一位并转换为string。 说字节有8位,我们可以通过位移一个接一个。 例如,我们将字节6位的第二位移到右边,将位的第二位移到最后的8位,然后用(&)用0x0001清除前面的位。

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