0

I'm having a trouble converting decimal to binary. How to convert binary using string Builder in adding 0's from the start?

while (num != 0){    
  int digit = num % 2;

  buf.append(digit);
  num = num/2;
}   

System.out.println(num);
Matt Ball
  • 354,903
  • 100
  • 647
  • 710
biiikmar
  • 1
  • 1
  • 1

5 Answers5

1

Decimal number can be converted to binary by dividing the number by 2 and noting down the remainder For example if you take 12 as decimal number binary number would be 1100

Manual Output:

2 12

2 6 0

2 3 0

2 1 1

1

Code to covert decimal to binary is

class Binary
    {
        public StringBuffer calBinary(int decnum){

          StringBuffer sBuf = new StringBuffer();
               int temp=0;
               while(decnum>0){
                      temp = decnum%2;
                      sBuf.append(temp);
                     decnum = decnum / 2;
               }
         return sBuf.reverse();
   } }  


 public class Sri {

   public static void main(String[] args) throws IOException {
    System.out.println(" please enter the decimal number to convert into binary");
    BufferedReader br = new   BufferedReader(new InputStreamReader(System.in));
    int num = Integer.parseInt(br.readLine());

    Binary K = new Binary();
    StringBuffer result = K.calBinary(decnum);
    System.out.println(result);
              }
 }
JAVA
  • 524
  • 11
  • 23
1

Try using the reverse() method from the string builder class. Something along the lines of:

buf = buf.reverse();
user2699231
  • 155
  • 1
  • 2
  • 10
1

You can try this

        int num=10;
    StringBuilder buf1=new StringBuilder();
    StringBuilder buf2=new StringBuilder();
    while (num != 0){
        int digit = num % 2;
        buf1.append(digit); // apend 0101 order
        num = num/2;
    }
    String binary=buf1.reverse().toString();// reverse to get binary 1010
    int length=binary.length();
    if(length<8){
       while (8-length>0){
           buf2.append("0");// add zero until length =8
           length++;
       }
    }
    String bin=buf2.toString()+binary;// binary string with leading 0's
    System.out.println(bin);

Live Demo here.

Out put

00001010

You can do the same thing with an array as follows

    int num=10;
    int[] binArr=new int[8];
    int j=0;
    while (num != 0){
        int digit = num % 2;
        binArr[binArr.length-1-j]=digit;
        num = num/2;
        j++;
    }
    System.out.println(Arrays.toString(binArr).replaceAll("\\,|\\[|\\]",""));
Ruchira Gayan Ranaweera
  • 34,993
  • 17
  • 75
  • 115
1

Try this (with minimum modifications to your original code)...

    int num=8;
    StringBuilder buf = new StringBuilder();
    while (num != 0){    
          int digit = num % 2;

          buf.append(digit);
          num = num/2;
        }   

        System.out.println(buf.reverse());
Farrukh Chishti
  • 7,652
  • 10
  • 36
  • 60
-1

If you're only allowed to append, you are probably supposed to be using recursion:

String toBinaryString(int num)
{
    StringBuilder sb = new StringBuilder();
    if (num > 1)
    {
        sb.append(decimalToBinary(num/2));    // or num >> 1
    }
    sb.append((num & 1));
    return sb.toString();
}

Not the world's most efficient code, but you did specify both StringBuilder and append only. Handling of negative numbers left as an exercise for the reader.

user207421
  • 305,947
  • 44
  • 307
  • 483