answersLogoWhite

0

Here's a simple Java method to perform the conversion from decimal to a binary string representation:
public String toBinaryString(int n) {
StringBuilder sb = new StringBuilder();
while(n != 0) {
sb.insert(0, n % 2 != 0 ? '1' : '0');
n /= 2;
}
return sb.toString();
}

The java.lang.Integer class provides a conversion helper method between decimal (integer) and binary numbers in string form called toBinaryString().

String binaryValue = Integer.toBinaryString(43) // 43-> "101011"

User Avatar

Wiki User

14y ago

Still curious? Ask our experts.

Chat with our AI personalities

RafaRafa
There's no fun in playing it safe. Why not try something a little unhinged?
Chat with Rafa
CoachCoach
Success isn't just about winning—it's about vision, patience, and playing the long game.
Chat with Coach
EzraEzra
Faith is not about having all the answers, but learning to ask the right questions.
Chat with Ezra
More answers

You can't. While Java supports hexadecimal and octal literals, there is no native support for binary. You need to use/write a method to do the conversions for you.

User Avatar

Wiki User

16y ago
User Avatar

Add your answer:

Earn +20 pts
Q: Write a Java program to convert a decimal no to binary?
Write your answer...
Submit
Still have questions?
magnify glass
imp