How to convert a byte to its binary string representation
For example, the bits in a byte B
was 10000010
, how can I assign the bits to the string str
literally, which is, str = "10000010"
.
byteB1 = (byte)129; String S1= String.Format ("%8s", Integer.tobinarystring (B1 &0xFF). Replace (' ','0'); System. out. println (S1);//10000001byteB2 = (byte)2; String S2= String.Format ("%8s", Integer.tobinarystring (B2 &0xFF). Replace (' ','0'); System. out. println (S2);//00000010
Public classbitssetcount{ Public Static voidMain (string[] args) {intSend =8549658; System. out. println ("[Input] Integer value:"+ Send +"\ n" ); Bitssetcount.countbits (send); } Private Static voidCountbits (inti) {System. out. println ("integer.tobinarystring:"+integer.tobinarystring (i)); System. out. println ("integer.tohexstring:"+integer.tohexstring (i)); System. out. println ("Integer.bitcount:"+Integer.bitcount (i)); intD = i &0xff000000; intc = i &0xff0000; intb = i &0xff00; intA = 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)); intall = 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 is sending the MSF bytes to a method * which would work on a single byte and print some * Bits We is 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 >>8) ) +" "+GetBits ((byte) (A >>0) ) ); } Private StaticString GetBits (byteinbyte) { //Go through each bit with a maskStringBuilder Builder =NewStringBuilder (); for(intj =0; J <8; J + + ) { //shift each bit by 1 starting at zero shift byteTMP = (byte) (Inbyte >>j); //Check byte with mask 00000001 for LSB intEXPECT1 = tmp &0x01; Builder.append (EXPECT1); } return(Builder.reverse (). toString ()); }}
Public StaticString bytetostring (byteb) {byte[] Masks = {- -, -, +, -,8,4,2,1 }; StringBuilder Builder=NewStringBuilder (); for(bytem:masks) { if((b & m) = =m) {builder.append ('1'); } Else{builder.append ('0'); } } returnbuilder.tostring ();}
Public Static String getbytebinarystring (byte b) { new StringBuilder (); for (int70;---i) {1) ; } return sb.tostring ();}
String bytetobinarystring (byteb) {StringBuilder Binarystringbuilder=NewStringBuilder (); for(inti =0; I <8; i++) Binarystringbuilder.append ( (0x80>>> i) & b) = =0?'0':'1'); returnbinarystringbuilder.tostring ();}
How to convert a byte to its binary string representation