1

Let's say i have a binary number initialized like this:

   int y=0b110101; 

How could i convert 110101 to a String? I would like this:

String str1 = Integer.toString(y); 

System.out.println(str1);

to give result 110101 or 0b110101 and not 53.

Epitheoritis 32
  • 366
  • 5
  • 13
  • 1
    `String str1 = Integer.toBinaryString(y);` – Eritrean Aug 13 '19 at 10:30
  • 3
    Possible duplicate of [Converting an int to a binary string representation in Java](https://stackoverflow.com/questions/2406432/converting-an-int-to-a-binary-string-representation-in-java) – Eritrean Aug 13 '19 at 10:38

1 Answers1

1

Integer.toBinaryString(y) would give you 110101 in your case and you can prepend the 0b to the result if you'd like that.

Valdrinium
  • 1,398
  • 1
  • 13
  • 28