Java code to convert from Base-10 to Base-9
Asked Answered
C

6

5

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

Casino answered 8/10, 2010 at 7:48 Comment(1)
A number does not have a base. A string representation of a number does have a base. When you say you have a number in base 10 you actually mean that you have a base 10 string representation of a number. The question is an oxymoron.Curium
V
10

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));
Vignette answered 8/10, 2010 at 8:40 Comment(0)
M
7
Long base10 = 10;
Long.valueOf(base10.toString(), 9);
Moule answered 8/10, 2010 at 7:50 Comment(6)
Actually i need it without having to convert it to string.Casino
No.it's not homework.I actually need to modify the function,to return base 9 with different number symbols.Casino
I don't really understand the context of the problem. Could you perhaps give an example in your question?Moule
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).Janiecejanifer
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".Curium
@Casino - please check my comment on your answer pointing out what you are actually doing.Syne
K
6

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?

Kincardine answered 8/10, 2010 at 8:13 Comment(0)
T
4

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)

Trackless answered 8/10, 2010 at 8:1 Comment(2)
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!Janiecejanifer
Fair enough, if you do it by hand :)Trackless
L
2

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; 
}
Lobell answered 8/10, 2010 at 8:18 Comment(3)
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.Jacobina
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.Lobell
I would not use sources from a site that uses state-the-obvious code documentation.Starry
G
0

If you LITERALLY can do anything but convert to string do the following:

public static long toBase(long num, int base) {
    long result;
    StringBuilder buffer = new StringBuilder();
    buffer.append(Long.toString(num, base));
    return Long.parseLong(buffer.toString());
}
Gracious answered 20/4, 2014 at 22:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.