Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Java how do you convert a decimal number to base 36?

Tags:

java

math

decimal

If I have a decimal number, how do I convert it to base 36 in Java?

like image 571
slavoj Avatar asked Feb 20 '11 23:02

slavoj


People also ask

How do you convert a number to a base 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.

How do you convert a decimal to a base number?

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.

How do you change a decimal to base 3?

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.


2 Answers

Given a number i, use Integer.toString(i, 36).

like image 198
Jeremiah Willcock Avatar answered Oct 12 '22 00:10

Jeremiah Willcock


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 
like image 34
RichardTheKiwi Avatar answered Oct 12 '22 00:10

RichardTheKiwi