Created
December 27, 2016 09:24
-
-
Save hmhmsh/c7a07e4eafa358bf31cb1f5aa3839dd2 to your computer and use it in GitHub Desktop.
Decimal10 to Decimal2 etc...
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.ArrayList; | |
import java.util.List; | |
import java.util.Collections; | |
class Decimal { | |
static int DECIMAL_10 = 20; | |
public static void main(String[] args) { | |
System.out.println(convertDecimal(DECIMAL_10, 2)); // 10100 | |
System.out.println(convertDecimal(DECIMAL_10, 8)); // 24 | |
System.out.println(convertDecimal(DECIMAL_10, 16)); // 14 | |
} | |
private static String convertDecimal(int num_10, int convertDecimalNum) { | |
List<Integer> decimal = new ArrayList<>(); | |
int num = num_10; | |
while (num > 0){ | |
decimal.add(num % convertDecimalNum); | |
num = num / convertDecimalNum; | |
} | |
Collections.reverse(decimal); | |
return ArrayToString(decimal); | |
} | |
private static String ArrayToString(List<Integer> array) { | |
StringBuilder buf = new StringBuilder(); | |
for(int index : array){ | |
buf.append(String.valueOf(index)); | |
} | |
return buf.toString(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment