# To binary number

 Arnab Banerjee · January 24, 2015 How to convert any number to its corresponding binary number using Java ?

## Replies

 Siddharth Venu · March 10, 2016 ``import java.lang.StringBuilder;import java.lang.Integer;public class DeciToBinary { public static int toBinary(int n){ int temp; int ans; StringBuilder builder=new StringBuilder(); for(temp=n;temp!=1;){ int remainder=temp%2; builder.append(remainder); temp/=2; } builder.append(1); builder.reverse(); String binary=builder.toString(); ans=Integer.parseInt(binary); return ans; } public static void main(String[] args) { System.out.println(toBinary(21)); System.out.println(toBinary(121));                System.out.println(toBinary(827)); }}``Tested, working fine. I used this method for the conversion. Source Gary Whitney · March 11, 2016 All numbers integers are binary. There just isn't a binary data type in Java.Run this code to see how Integer.toBinary() works.``        int binAND, binOR, binXOR;        String strBin3, strBin7, strBinAND, strBinOR,strBinXOR;        strBin3 =(Integer.toBinaryString(3));        strBin7 =(Integer.toBinaryString(7));        binAND = 3 & 7;        binOR = 3 | 7;        binXOR = 3 ^ 7;        strBinAND = (Integer.toBinaryString(binAND));        strBinOR = (Integer.toBinaryString(binOR));        strBinXOR = (Integer.toBinaryString(binXOR));        System.out.printf("bin3 = %s and bin7 = %s.\n",strBin3, strBin7);        System.out.println("strBinAND = "  + strBinAND);        System.out.println("strBinOR = "  + strBinOR);        System.out.println("strBinXOR = "  + strBinXOR);``
• 1

## Java / Android Development

129,765 followers
About

Very popular language used to create desktop applications, website applets, and Android apps.

Links
Moderators
 Bucky Roberts Administrator