If I have a decimal number, how do I convert it to base 36 in Java?
A method called convertFromDecimal which takes two integers as parameters. The first integer is the number to be converted and the second integer is the base to be converted to. The base value could be any number between 2 (binary) and 16 (hexadecimal). The converted number is returned as a String.
Decimal to Other Base SystemStep 1 − Divide the decimal number to be converted by the value of the new base. Step 2 − Get the remainder from Step 1 as the rightmost digit (least significant digit) of new base number. Step 3 − Divide the quotient of the previous divide by the new base.
Steps to Convert Decimal to Ternary:Divide the number by 3. Get the integer quotient for the next iteration. Get the remainder for the ternary digit. Repeat the steps until the quotient is equal to 0.
Given a number i
, use Integer.toString(i, 36)
.
See the documentation for Integer.toString
http://docs.oracle.com/javase/7/docs/api/java/lang/Integer.html#toString(int,%20int)
toString public static String toString(int i, int radix) .... The following ASCII characters are used as digits: 0123456789abcdefghijklmnopqrstuvwxyz
What is radix
? You're in luck for Base 36 (and it makes sense)
http://docs.oracle.com/javase/7/docs/api/java/lang/Character.html#MAX_RADIX
public static final int MAX_RADIX 36
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With