用户提供需要转换的数据和该数据的进制,以及要转换的进制
    例如,用户提供了一个十进制数:10,要求将此数据以二进制形式转换,则通过进制转换器转换的最终结果应该:1010。
    提示:此进制转换器可以在 2-36 进制之间对数据进行任意转换。各进制中对应的数字如下表:

    进制转换器 - 图1

    1. package com.structure;
    2. import org.junit.Assert;
    3. import org.junit.Test;
    4. public class NumberSystemChange {
    5. /** 定义一个顺序栈结构:数组 + 栈顶指针 */
    6. char[] data;
    7. int s_top = -1;
    8. public String numberSystemChange(String numberSrc,int srcSystem,int newSystem){
    9. // 先转换为10进制
    10. int tenNumber = toTenSystem(numberSrc,srcSystem);
    11. // 通过计算对数,来求得大致的长度 以 newSystem为底,求numberSrc的对数然后加一
    12. int length = (int) (Math.log(tenNumber)/Math.log(newSystem)) +1;
    13. System.out.println("长度:"+length);
    14. data = new char[length];
    15. toNewSystem(tenNumber,newSystem);
    16. return toSystem();
    17. }
    18. private void toNewSystem(int tenNumber,int newSystem){
    19. // 再求各位上的进制值,然后压入栈中(从低位开始计算)
    20. while (tenNumber/newSystem>0) {
    21. push(tenNumber%newSystem);
    22. tenNumber/=newSystem;
    23. }
    24. push(tenNumber%newSystem);
    25. }
    26. private String toSystem(){
    27. StringBuilder sb = new StringBuilder();
    28. for(;s_top>-1;s_top--){
    29. sb.append(data[s_top]);
    30. }
    31. return sb.toString();
    32. }
    33. /** 倒转 */
    34. private void inverted(){
    35. if(s_top<1){
    36. return;
    37. }
    38. for(int i = 0;i<=s_top/2;i++){
    39. char temp = data[i];
    40. data[i] = data[s_top-i];
    41. data[s_top-i] = temp;
    42. }
    43. }
    44. private void push(int number){
    45. if(number > 9){
    46. data[++s_top] = (char)(number+55);
    47. }else{
    48. data[++s_top] = (char)(number+48);
    49. }
    50. }
    51. /** 转换为10进制 */
    52. public int toTenSystem(String numberSrc,int srcSystem){
    53. char[] srcdata = numberSrc.toCharArray();
    54. int length = srcdata.length;
    55. int tenSystem_data = 0;
    56. for(int i = length-1;i>=0;i--){
    57. int temp ;
    58. // 转换小写字母为10进制 a的ascII码对应97 要对应 10
    59. if(srcdata[i] >= 97){
    60. temp = srcdata[i] - 87;
    61. }
    62. // 转换大写字母为10进制 A 要对应 10
    63. else if(srcdata[i] >= 65 ){
    64. temp = srcdata[i]-55;
    65. }
    66. // 验证数字 1要对应1 1的ASCII码对应49
    67. else{
    68. temp = srcdata[i]-48;
    69. }
    70. tenSystem_data += temp*Math.pow(srcSystem,length-1-i);
    71. }
    72. return tenSystem_data;
    73. }
    74. @Test
    75. public void testAscII(){
    76. char a = 'a';
    77. char A = 'A';
    78. char c = '1';
    79. // 97
    80. System.out.println((int)a);
    81. // 65
    82. System.out.println((int)A);
    83. // 49
    84. System.out.println((int)c);
    85. }
    86. @Test
    87. public void testToten(){
    88. NumberSystemChange nsc = new NumberSystemChange();
    89. String number1 = "A";
    90. int tenNumber1 = nsc.toTenSystem(number1,16);
    91. Assert.assertEquals(tenNumber1,10);
    92. number1 = "a";
    93. tenNumber1 = nsc.toTenSystem(number1,16);
    94. Assert.assertEquals(tenNumber1,10);
    95. number1 = "1101";
    96. tenNumber1 = nsc.toTenSystem(number1,2);
    97. Assert.assertEquals(tenNumber1,13);
    98. }
    99. @Test
    100. public void testSystemChange(){
    101. NumberSystemChange nsc = new NumberSystemChange();
    102. System.out.println(nsc.numberSystemChange("16",10,16));
    103. NumberSystemChange nsc2 = new NumberSystemChange();
    104. System.out.println(nsc2.numberSystemChange("11A1",12,16));
    105. NumberSystemChange nsc3 = new NumberSystemChange();
    106. System.out.println(nsc3.numberSystemChange("11A1",13,16));
    107. NumberSystemChange nsc4 = new NumberSystemChange();
    108. System.out.println(nsc4.numberSystemChange("AAAAEEEAAAEEE",16,10));
    109. }
    110. @Test
    111. public void testlog(){
    112. System.out.println((int) (Math.log(5)/Math.log(2)) +1);
    113. System.out.println((int) (Math.log(8)/Math.log(10)) +1);
    114. System.out.println((int) (Math.log(10)/Math.log(10)) +1);
    115. System.out.println((int) (Math.log(16)/Math.log(10)) +1);
    116. System.out.println((int) (Math.log(100)/Math.log(2)) +1);
    117. System.out.println((int) (Math.log(1000)/Math.log(2)) +1);
    118. System.out.println((int) (Math.log(1024)/Math.log(2)) +1);
    119. }
    120. }