0

I am working in Android.

I have convert the value from the Hex to the Binary.

For example :

String Hex = "02";
String Bin = Integer.toBinaryString(Integer.parseInt(Hex, 16));
Log.e(WifiP2P.TAG, "Binary is " + Bin);

The log show like the following:Binary is 10.

How to add the 0 after convert to the Binary for 8 bit ? Like 00000010.

Thanks in advance.

Martin
  • 2,693
  • 11
  • 37
  • 62

1 Answers1

3

You can do something like:

//Bin is your variable containing the initial binary string 
String.format("%8s", Bin).replace(' ', '0'); //8 indicates the length of the binary string

It will pad 0s to the remaining empty spaces.