用户提供需要转换的数据和该数据的进制,以及要转换的进制
例如,用户提供了一个十进制数:10,要求将此数据以二进制形式转换,则通过进制转换器转换的最终结果应该:1010。
提示:此进制转换器可以在 2-36 进制之间对数据进行任意转换。各进制中对应的数字如下表:
package com.structure;
import org.junit.Assert;
import org.junit.Test;
public class NumberSystemChange {
/** 定义一个顺序栈结构:数组 + 栈顶指针 */
char[] data;
int s_top = -1;
public String numberSystemChange(String numberSrc,int srcSystem,int newSystem){
// 先转换为10进制
int tenNumber = toTenSystem(numberSrc,srcSystem);
// 通过计算对数,来求得大致的长度 以 newSystem为底,求numberSrc的对数然后加一
int length = (int) (Math.log(tenNumber)/Math.log(newSystem)) +1;
System.out.println("长度:"+length);
data = new char[length];
toNewSystem(tenNumber,newSystem);
return toSystem();
}
private void toNewSystem(int tenNumber,int newSystem){
// 再求各位上的进制值,然后压入栈中(从低位开始计算)
while (tenNumber/newSystem>0) {
push(tenNumber%newSystem);
tenNumber/=newSystem;
}
push(tenNumber%newSystem);
}
private String toSystem(){
StringBuilder sb = new StringBuilder();
for(;s_top>-1;s_top--){
sb.append(data[s_top]);
}
return sb.toString();
}
/** 倒转 */
private void inverted(){
if(s_top<1){
return;
}
for(int i = 0;i<=s_top/2;i++){
char temp = data[i];
data[i] = data[s_top-i];
data[s_top-i] = temp;
}
}
private void push(int number){
if(number > 9){
data[++s_top] = (char)(number+55);
}else{
data[++s_top] = (char)(number+48);
}
}
/** 转换为10进制 */
public int toTenSystem(String numberSrc,int srcSystem){
char[] srcdata = numberSrc.toCharArray();
int length = srcdata.length;
int tenSystem_data = 0;
for(int i = length-1;i>=0;i--){
int temp ;
// 转换小写字母为10进制 a的ascII码对应97 要对应 10
if(srcdata[i] >= 97){
temp = srcdata[i] - 87;
}
// 转换大写字母为10进制 A 要对应 10
else if(srcdata[i] >= 65 ){
temp = srcdata[i]-55;
}
// 验证数字 1要对应1 1的ASCII码对应49
else{
temp = srcdata[i]-48;
}
tenSystem_data += temp*Math.pow(srcSystem,length-1-i);
}
return tenSystem_data;
}
@Test
public void testAscII(){
char a = 'a';
char A = 'A';
char c = '1';
// 97
System.out.println((int)a);
// 65
System.out.println((int)A);
// 49
System.out.println((int)c);
}
@Test
public void testToten(){
NumberSystemChange nsc = new NumberSystemChange();
String number1 = "A";
int tenNumber1 = nsc.toTenSystem(number1,16);
Assert.assertEquals(tenNumber1,10);
number1 = "a";
tenNumber1 = nsc.toTenSystem(number1,16);
Assert.assertEquals(tenNumber1,10);
number1 = "1101";
tenNumber1 = nsc.toTenSystem(number1,2);
Assert.assertEquals(tenNumber1,13);
}
@Test
public void testSystemChange(){
NumberSystemChange nsc = new NumberSystemChange();
System.out.println(nsc.numberSystemChange("16",10,16));
NumberSystemChange nsc2 = new NumberSystemChange();
System.out.println(nsc2.numberSystemChange("11A1",12,16));
NumberSystemChange nsc3 = new NumberSystemChange();
System.out.println(nsc3.numberSystemChange("11A1",13,16));
NumberSystemChange nsc4 = new NumberSystemChange();
System.out.println(nsc4.numberSystemChange("AAAAEEEAAAEEE",16,10));
}
@Test
public void testlog(){
System.out.println((int) (Math.log(5)/Math.log(2)) +1);
System.out.println((int) (Math.log(8)/Math.log(10)) +1);
System.out.println((int) (Math.log(10)/Math.log(10)) +1);
System.out.println((int) (Math.log(16)/Math.log(10)) +1);
System.out.println((int) (Math.log(100)/Math.log(2)) +1);
System.out.println((int) (Math.log(1000)/Math.log(2)) +1);
System.out.println((int) (Math.log(1024)/Math.log(2)) +1);
}
}