注:进位制转换的规则就是通过短除法将余数换算成0-9,a-z,A-Z 然后相反方法互转
一、环境(资源中有)
1.idea 2018.1
2.jdk1.8
3.maven 3.3.3
二、pom.xml
<?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.ygq</groupId>
<artifactId>pdf_utils</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.icepdf.os/icepdf-core -->
<dependency>
<groupId>org.icepdf.os</groupId>
<artifactId>icepdf-core</artifactId>
<version>6.2.2</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
</dependencies>
</project>
三、MathUtil.java
package com.ygq.utils;
import java.awt.*;
import java.math.BigInteger;
import java.util.Scanner;
public class MathUtil {
public static void main(String[] args) {
System.out.println("本程序为进位制转换操作!");
while (true) {
System.out.println("请输入您要进行的操作:1、十进制转62进制 2、62进制转十进制 0、退出");
Scanner sc = new Scanner(System.in);
String choose = sc.next();
if (choose.equals("1")) {
tenTo64Method();
} else if (choose.equals("2")) {
sixtyFourToTenMethod();
} else if (choose.equals("0")) {
System.out.println("谢谢使用!");
break;
} else {
System.out.println("请输入正确的范围(0-2)");
}
}
}
public static void tenTo62Method() {
char[] depository = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};
//todo 临时
System.out.println("当前操作:十进制转62进制");
System.out.println("请输入要转换的十进制数字:");
Scanner sc = new Scanner(System.in);
String next = sc.next();
BigInteger num = new BigInteger(next);
BigInteger bint62 = new BigInteger(String.valueOf(62));//除数
StringBuffer stringBuffer = new StringBuffer(); //拼接字符串
while (true) {
BigInteger remainder = new BigInteger(String.valueOf(0)); //定义余数
//int dividend = 0;//定义被除数
remainder = num.mod(bint62); //求余数
stringBuffer.append(depository[remainder.intValue()]); //根据余数取对应的字符(0-61)
num = num.divide(bint62);
if (num.intValue() == 0) break;
}
//将字符串进行翻转
stringBuffer.reverse();
System.out.println("结果为" + stringBuffer);
}
public static void sixtyTwoToTenMethod() {
//定义仓库
String[] depository = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"};
System.out.println("当前操作:62进制转十进制");
System.out.println("请输入要转换的62进制数字:");
Scanner sc = new Scanner(System.in);
String num = sc.next();
//输入的数据
// String num = "4zXyLE1Gv";
BigInteger jieguo = new BigInteger(String.valueOf(0));
String result[] = new String[62];
StringBuffer stringBuffer = new StringBuffer();
stringBuffer.append(num);
StringBuffer reverse = stringBuffer.reverse();
BigInteger bint62 = new BigInteger(String.valueOf(62));
for (int i = 0; i < reverse.length(); i++) {
String substr = reverse.substring(i, i + 1);// q,t,k,P
result[i] = substr;
for (int j = 0; j < depository.length; j++) {
if ((result[i]).equals(depository[j])) {
jieguo = (jieguo.add(bint62.pow(i).multiply(new BigInteger(String.valueOf(j)))));
continue;
}
}
}
System.out.println("结果为:" + jieguo);
}
}
四、运行结果
本程序为进位制转换操作!
请输入您要进行的操作:1、十进制转62进制 2、62进制转十进制 0、退出
1
当前操作:十进制转62进制
请输入要转换的十进制数字:
99999999999999999
结果为7o044QyIzB
请输入您要进行的操作:1、十进制转62进制 2、62进制转十进制 0、退出
2
当前操作:62进制转十进制
请输入要转换的62进制数字:
7o044QyIzB
结果为:99999999999999999
请输入您要进行的操作:1、十进制转62进制 2、62进制转十进制 0、退出
4
请输入正确的范围(0-2)
请输入您要进行的操作:1、十进制转62进制 2、62进制转十进制 0、退出
0
谢谢使用!Process finished with exit code 0