Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
d-william committed Nov 19, 2019
0 parents commit 03a3b4d
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Project exclude paths
/target/
29 changes: 29 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.infinity.commons</groupId>
<artifactId>number-converter</artifactId>
<version>1.0.0</version>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>8</source>
<target>8</target>
</configuration>
</plugin>
</plugins>
</build>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

</project>
76 changes: 76 additions & 0 deletions src/main/java/com/infinity/converter/NumberConverter.java
Original file line number Diff line number Diff line change
@@ -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;
}

}

0 comments on commit 03a3b4d

Please sign in to comment.