Jump to content

badly need help. in Converting decimal to binary number system in java.

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
3 replies to this topic

#1
flickbyk_cyrus

flickbyk_cyrus

    Newbie

  • Members
  • Pip
  • 4 posts
This method basically converts decimal to binary but i couldn't trace how this method works.

can anyone explain this to me? especially "math.floor". i really dont understand what it does...

here's the code(method),

public static String convert(int n, int b)
{
int a = 0;
String s = "";
String z = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";

while(n != 0)
{
a = n % b;
s = z.charAt(a) + s;
n = (int) Math.floor(n / b);
}

return s;

}

hoping for your replies and help..tnx in advance guys.
pls help me.

#2
tate

tate

    Learning Programmer

  • Members
  • PipPipPip
  • 90 posts
Here is what the declaration on math.floor says

Quote

* Returns the largest (closest to positive infinity)
* <code>double</code> value that is less than or equal to the
* argument and is equal to a mathematical integer. Special cases:
* <ul><li>If the argument value is already equal to a
* mathematical integer, then the result is the same as the
* argument. <li>If the argument is NaN or an infinity or
* positive zero or negative zero, then the result is the same as
* the argument.</ul>
For your first question I am not sure yet.
twas brillig

#3
kmhosny

kmhosny

    Programmer

  • Members
  • PipPipPipPip
  • 133 posts
this function converts from decimal to any numbering system not just binary,
as for binary if you traced it the line
a=n%b
with b as base which will be 2 the output will only be 0 or 1 accessing the string with 0 or 1 will return 0 or 1 :) for the case of base 2, so the final string "s" will contain 0 or 1 and the last line
n = (int) Math.floor(n / b);
is the normal conversion for decimal to binary where you divide by 2 and take the integer part "thats why used floor".
floor in general approximate the number to the first one below
e.g :2.57---floor--->2

PS: try to trace your codes by placing break points and monitoring memory changes done by the lines of code

#4
G1TN3RD

G1TN3RD

    Newbie

  • Members
  • Pip
  • 8 posts
Are you asking what that code does?

It converts from decimal to any base, The reason it has two loops is so that it can get each number after the other after one (whichever) is input into the program. any can be input because String Z was assigned all possible decimal inputs.

As for your specific question.. math.floor rounds a number DOWN... (hance the .floor, haha get it)

for ex. document.write(Math.floor(0.30)); would be "0"

String s is an empty string, a placeholder, nothing.
The reason a = n%b is because you need to get the remainder of a number after it is divided by 2 so that you can output that remainder. (for ex. if its base2, its going to output the remainders which should all either be a 1 or a 0)

Hope this clears up what the code does for you. And i hope that was your question... hahaha...

-Git