views:

144

answers:

5

How to convert a long number in base 10 to base 9 without converting to string ?

+5  A: 
Long base10 = 10;
Long.valueOf(base10.toString(), 9);
mR_fr0g
Actually i need it without having to convert it to string.
Emil
is it homework?
mR_fr0g
i edited the question to make it clear.
Emil
No.it's not homework.I actually need to modify the function,to return base 9 with different number symbols.
Emil
I don't really understand the context of the problem. Could you perhaps give an example in your question?
mR_fr0g
Unless it's homework, going via string is the smartest route, unless you have fierce performance requirements. Otherwise, you'd just have to reimplement the algorithm used by `valueOf()` (essentially, using modulus and integer division to pick out base-9 digits from your base number).
Pontus Gagge
No, you're doing it the wrong way around. You're interpreting the string "10" as a base 9 representation. I believe the point of the question was to go from base 10 "9" to base 9 "10", but your answer does the opposite. I think you want `Long.toString(9, 9)`, which returns `"10"`.
Christoffer Hammarström
@Emil - please check my comment on your answer pointing out what you are actually doing.
Stephen C
+3  A: 

You can't convert to base 9 without converting to string.

When you write

Long a = 123;

you're making the implicit assumption that it's in base 10. If you want to interpret that as a base 9 number that's fine, but there's no way Java (or any other language I know of) is suddenly going to see it that way and so 8+1 will return 9 and not 10. There's native support for base 2, 8, 16 and 10 but for any other base you'll have to treat it as a string. (And then, if you're sure you want this, convert it back to a long)

Michael Clerx
Technically, you **can** do it without `string` using integer arithmetic, but it's hard to see why you should. You can go either way: translating from (calculating 1*9^2+2*9+3) or translating to (producing decimal 148 intended to be interpreted as the base-9 equivalent). I really don't see the point, though!
Pontus Gagge
Fair enough, if you do it by hand :)
Michael Clerx
+5  A: 

What does "convert to base 9 without converting to string" actually mean?

Base-9, base-10, base-2 (binary), base-16 (hexadecimal), are just ways to represent numbers. The value itself does not depend on how you represent it. int x = 256 is exactly the same as int x = 0xff as far as the compiler is concerned.

If you don't want to "convert to string" (I read this as meaning you are not concerned with the representation of the value), then what do you want to do exactly?

Grodriguez
+2  A: 

You have to apply the algorithm that converts number from one base to another by applying repeated modulo operations. Look here for a Java implementation. I report here the code found on that site. The variable M must contain the number to be converted, and N is the new base. Caveat: for the snippet to work properly, N>=1 && N<=10 must be true. The extension with N>10 is left to the interested reader (you have to use letters instead of digits).

String Conversion(int M, int N) // return string, accept two integers
{   
    Stack stack = new Stack();  // create a stack
    while (M >= N)  // now the repetitive loop is clearly seen
    {   
        stack.push(M mod N);    // store a digit
        M = M/N;    // find new M
    }   
    // now it's time to collect the digits together 
    String str = new String(""+M);  // create a string with a single digit M
    while (stack.NotEmpty())    
        str = str+stack.pop()   // get from the stack next digit
    return str; 
}
Luca Martini
M and N aren't the clearest variable names! I assume M is the number to be converted and N is the new base. Interesting though.
Adrian Mouat
I just copied the program that I found on the linked site. Indeed M is the number to be converted an N is the new base.
Luca Martini
I would not use sources from a site that uses state-the-obvious code documentation.
Arian
+2  A: 

FWIW, all values are actually in base 2 inside your machine (I bet you already knew that). It only shows up as base 10 because string conversion creates string representations in base 10 (e.g. when you print), because methods like parseLong assumes the input string is in base 10 and because the compiler expects all literals to be in base 10 when you actually write code. In other words, everything is in binary, the computer only converts stuff into and from base 10 for the convenience of us humans.

It follows that we should be easily able to change the output base to be something other than 10, and hence get string representations for the same value in base 9. In Java this is done by passing an optional extra base parameter into the Long.toString method.

long x=10;
System.out.println(Long.toString(x,9));
MAK