Java code to convert from Base-10 to Base-9

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

+4
source share
6 answers

FWIW, all values โ€‹โ€‹are actually in base 2 inside your machine (I'm sure you already knew that). It only appears as base 10, because string conversion creates string representations in base 10 (for example, when printing), because methods like parseLong assume that the input string is in base 10 and because the compiler expects everything literals will be in base 10 when you actually write code. In other words, everything is in binary form, the computer only converts the material to and from base 10 for the convenience of people.

It follows that we should easily change the source base to something other than 10, and therefore get string representations for the same value in base 9. In Java, this is done by passing an optional additional base parameter to Long.toString .

 long x=10; System.out.println(Long.toString(x,9)); 
+8
source
 Long base10 = 10; Long.valueOf(base10.toString(), 9); 
+6
source

What does โ€œconvert to base 9 without converting to stringโ€ mean?

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

If you do not want to "convert to string" (I read it as meaning that you are not interested in representing the value), then what do you want to do exactly?

+6
source

You cannot convert to base 9 without converting to a string.

When you write

 Long a = 123; 

you make the implicit assumption that it is in base 10. If you want to interpret it as base number 9, which is great, but there is no way that Java (or any other language that I know about) will suddenly see it this way, and therefore 8 + 1 will return 9, not 10. There is built-in support for bases 2, 8, 16 and 10, but for any other base you will have to consider it as a string. (And then, if you are sure you want this, return it to a long one)

+4
source

You must apply an algorithm that converts a number from one base to another using repetitive modulo operations. Look here for a Java implementation. I am reporting here the code found on this site. The variable M must contain the number to be converted, and N be the new base. Caution: for the fragment to work correctly, N>=1 && N<=10 must be true. An extension with N>10 provided to the interested reader (you should use letters instead of numbers).

 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 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; } 
+2
source

If you can do LITERALLY anything other than converting to a string, follow these steps:

 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()); } 
0
source

All Articles