-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 03a3b4d
Showing
3 changed files
with
107 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
# Project exclude paths | ||
/target/ |
This file contains 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
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> |
This file contains 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
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; | ||
} | ||
|
||
} |