diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..744289d --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +# Project exclude paths +/target/ \ No newline at end of file diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..b8d74d0 --- /dev/null +++ b/pom.xml @@ -0,0 +1,29 @@ + + + 4.0.0 + + com.infinity.commons + number-converter + 1.0.0 + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.8.1 + + 8 + 8 + + + + + + + UTF-8 + + + \ No newline at end of file diff --git a/src/main/java/com/infinity/converter/NumberConverter.java b/src/main/java/com/infinity/converter/NumberConverter.java new file mode 100644 index 0000000..bb8a1c2 --- /dev/null +++ b/src/main/java/com/infinity/converter/NumberConverter.java @@ -0,0 +1,76 @@ +package com.infinity.converter; + +import java.math.BigInteger; + +public class NumberConverter { + + public static final String BINAIRE = "01"; + public static final String TRINAIRE = "012"; + public static final String OCTAL = "01234567"; + public static final String NONAIRE = "012345678"; + public static final String DECIMAL = "0123456789"; + public static final String DUODECIMAL = "0123456789AB"; + public static final String HEXADECIMAL = "0123456789ABCDEF"; + public static final String VEGESIMAL = "0123456789ABCDEFGHIJ"; + + public static boolean uniqueChar(String str) { + for (int i = 0; i < str.length(); i++) + for (int j = i + 1; j < str.length(); j++) + if (str.charAt(i) == str.charAt(j)) + return false; + return true; + } + + private static boolean charExist(String inputNumber, String digits) { + for (int i = 0; i < inputNumber.length(); i++) + if (digits.indexOf(inputNumber.charAt(i)) == -1) return false; + return true; + } + + public static BigInteger toDec(String digits, String inputNumber){ + if (!uniqueChar(digits)) return null; + if (!charExist(inputNumber, digits)) return null; + int length = inputNumber.length(); + int base = digits.length(); + BigInteger power = BigInteger.ONE; + BigInteger num = BigInteger.ZERO; + for (int i = length - 1; i >= 0; i--) { + BigInteger temp = new BigInteger(Integer.toString(digits.indexOf(inputNumber.charAt(i)))); + num = num.add(temp.multiply(power)); + power = power.multiply(new BigInteger(Integer.toString(base))); + } + return num; + } + + public static BigInteger binToDec(String number) { + return toDec(BINAIRE,number); + } + + public static BigInteger hexToDec(String number) { + return toDec(HEXADECIMAL,number); + } + + public static String fromDec(String digits, BigInteger number) { + if (!uniqueChar(digits)) return null; + BigInteger base = new BigInteger(Integer.toString(digits.length())); + StringBuilder sb = new StringBuilder(); + do { + sb.append(digits.charAt(number.mod(base).intValue())); + number = number.divide(base); + System.out.println(); + } + while (number.compareTo(BigInteger.ZERO) > 0); + return sb.reverse().toString(); + } + + public static String anyToAny(String inputDigits, String outputDigits, String inputNumber) { + return fromDec(outputDigits, toDec(inputDigits, inputNumber)); + } + + public static boolean isValid(String digits, String number) { + for (int i = 0; i < number.length(); i++) + if (!digits.contains(Character.toString(number.charAt(i)))) return false; + return true; + } + +}