-1

This is my function in Java:

public static String convertFromDecimal(int number, int base)
    {

        String result = "";

        /*
         * This while loop will keep running until 'number' is not 0
         */

        while(number != 0)
        {
            result = (number%base) + result; // Appending the remainder
            number = number / base; // Dividing the number by the base so we can get the next remainder
        }

        // If the number is already 0, then the while loop will ignore it, so we will return "0"

        if(result == "") 
        {
            return "0";
        }

        return result;

    }

It works fine for numbers that convert to numbers not beginning with 0, if the number is supposed to have a zero at the start, it will not record it, could anyone tell me why?

For example, if I print out

convertFromDecimal(13,2) it returns

1101

Which is correct, but if I print out

convertFromDecimal(461,2), I get

111001101

Where the actual answer is

0000000111001101

So it's the same as my answer without the leading zeroes, if anyone knows why I would appreciate the help, thank you.

EDIT My question is different because I don't want 16 digits, I want the binary number of the given decimal, a calculator like this can explain what I want.

Sam Chahine
  • 530
  • 1
  • 17
  • 52
  • Sounds like you should do some debugging. – Oliver Charlesworth Mar 07 '16 at 00:40
  • 2
    Well, if you want to print a 16-bit binary value, and 461 can be represented in 9 bits, your loop ends. Why do you think the string will be any longer? You need to get the length of your result, and pad with as many 0's as needed. – OldProgrammer Mar 07 '16 at 00:41
  • 1
    Possible duplicate of [How to get 0-padded binary representation of an integer in java?](http://stackoverflow.com/questions/4421400/how-to-get-0-padded-binary-representation-of-an-integer-in-java) – Chris Kitching Mar 07 '16 at 00:41
  • @OldProgrammer So the program thinks the loop should end because of the fact there are only zeroes? Is that what you mean? – Sam Chahine Mar 07 '16 at 00:43
  • "Use the Debugger, Luke..." – OldProgrammer Mar 07 '16 at 00:43
  • 1
    "The" binary representation of a number has no leading zeroes, just like "the" decimal representation of `42` is never written (or returned, unless specifically asked for) as `000042`. At *some* point you *must* tell your program how many digits to return. – Jongware Mar 07 '16 at 00:49
  • Oh.. So my program works fine? How come the calculator I linked gives extra zeroes at the front? @RadLexus – Sam Chahine Mar 07 '16 at 00:50
  • It creates groups of 8. – Jongware Mar 07 '16 at 00:51
  • Oh, okay, that makes a lot of sense. Just to clarify, there is nothing wrong with my program, and the only reason it doesn't have leading zeroes is because I didn't tell it to? – Sam Chahine Mar 07 '16 at 00:52

2 Answers2

0

I assume you are looking to format all your answers as shorts (16 bits).

In this case, simply check the length of your current string, and add on zeroes as needed.

int zeroesRemaining = 16 - result.length();
for (int i = 0; i < zeroesRemaining; i++) {
    result = "0" + result;
}

Alternatively, if you want to do it faster, use a StringBuilder.

int zeroesRemaining = 16 - result.length();
StringBuilder tempBuilder = new StringBuilder(result);
for (int i = 0; i < zeroesRemaining; i++) {
    tempBuilder.insert(0, 0); //inserts the integer 0 at position 0 of the stringbuilder
}
return tempBuilder.toString(); //converts to string format

There is also probably a formatter that could do this, but I don't know of such.

If you want to change the number of zeroes to be the closest integer primitive, just set zeroesRemaining to be the (least power of 2 that is greater than the number of bits) minus (the number of bits).

  • What if I want to convert 23 to binary, the answer is `00010111` but now my function would print out `0000000000010111` – Sam Chahine Mar 07 '16 at 00:46
  • Just change zeroesRemaining to the smallest power of 2 that is greater than the number of digits minus the number of digits. – questioner and answerer Mar 07 '16 at 00:48
  • 1
    Sorry but that didn't make any sense, if I change the zeroesRemaining to the smallest power of 2, that is greater than the number of digits minus the number of digits... (greater than the number of digits minus the number of digits) = 0.. no? Please correct me if I'm wrong – Sam Chahine Mar 07 '16 at 00:51
  • (smallest power of 2 that is greater than number of digits) minus (number of digits) – questioner and answerer Mar 07 '16 at 00:52
  • Oh okay, I get it now. Also, someone in the comments explained that it's my program is okay, but since your answer is correct to do with the question I asked (not really knowing what I was asking), I will accept your answer so that it can help future dwellers, thank you for your help! – Sam Chahine Mar 07 '16 at 00:54
0

Since you want fixed lengths for your result, in groups of 8 bits, the easiest way is to append 0 to the front of your result until its length is a multiple of 8.

That is as simple as

wile (result.length() % 8 > 0) 
{
    result = "0" + result;
}
return result;
Jongware
  • 22,200
  • 8
  • 54
  • 100