How to convert a long number in base 10 to base 9 without converting to string ?
-
9A 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. – Christoffer Hammarström Oct 08 '10 at 08:58
6 Answers
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));

- 26,140
- 11
- 55
- 86
Long base10 = 10;
Long.valueOf(base10.toString(), 9);

- 8,462
- 7
- 39
- 54
-
-
No.it's not homework.I actually need to modify the function,to return base 9 with different number symbols. – Emil Oct 08 '10 at 07:56
-
I don't really understand the context of the problem. Could you perhaps give an example in your question? – mR_fr0g Oct 08 '10 at 08:03
-
2Unless 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 Oct 08 '10 at 08:22
-
1No, 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 Oct 08 '10 at 09:41
-
@Emil - please check my comment on your answer pointing out what you are actually doing. – Stephen C Oct 08 '10 at 10:41
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?

- 21,501
- 10
- 63
- 107
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)

- 2,928
- 2
- 33
- 47
-
2Technically, 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 Oct 08 '10 at 08:26
-
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;
}

- 1,434
- 1
- 15
- 35
-
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 Oct 08 '10 at 08:39
-
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 Oct 08 '10 at 08:44
-
2I would not use sources from a site that uses state-the-obvious code documentation. – Cephalopod Oct 08 '10 at 09:25
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());
}

- 3,268
- 2
- 18
- 25