本帖最后由 syw02014 于 2014-3-12 09:59 编辑
这个有点麻烦。功能给你扩展一下,任意进制(进制是2到62进制之间)的数互转 我给你的这个程序可以输入输出1000000位,(程序中可能出现字母的意思: A = 10, B = 11, ..., Z = 35, a = 36, b = 37, ..., z = 61 )我想应该够你用的:
- /*
- 该程序的功能:
- 任意进制的数互转(进制:2-62);
- 首先输入该程序循环的次数T,再输入输入数的进制n,最后要转换的数s;(可根据程序提示输入)
- */
- import java.util.*;
- import java.io.*;
- import java.math.*;
- public class Main
- {
- public static void main(String[] args)
- {
- int i,j;
- Scanner cin = new Scanner(System.in);
- System.out.println("该程序循环的次数:");
- int T = cin.nextInt(); //T是该程序循环的次数
- while((T--)!=0)
- {
- System.out.println("输入数的进制:");
- int n = cin.nextInt(); //n:输入数的进制
-
- System.out.println("要转换的进制:");
- int m = cin.nextInt(); //m:要转换的进制
-
- System.out.println(n+"要转换的"+m+"进制的数:");
- String s = cin.next(); //s:要转换的数
-
- int l = s.length();
- BigInteger sum = BigInteger.valueOf(0);
- for(i=0;i<l;i++)
- {
- sum=sum.multiply(BigInteger.valueOf(n));
- sum=sum.add(BigInteger.valueOf(v(s.charAt(i))));
- }
-
- i=0;
- int a[] = new int[1000000];
- while(sum.compareTo(BigInteger.valueOf(0))!=0)
- {
- a[i]=sum.mod(BigInteger.valueOf(m)).intValue();
- sum=sum.divide(BigInteger.valueOf(m));
- i++;
- }
-
- System.out.println(n+"进制要转换"+m+"进制的数:"+s);
- System.out.print(n+"进制要转换"+m+"进制的数的结果:");
-
- for(j=i-1;j>=0;j--)
- {
- if(a[j]>=0&&a[j]<=9)
- System.out.print(a[j]);
- else if(a[j]>=10&&a[j]<=35)
- System.out.print((char)(a[j]-10+'A'));
- else if(a[j]>=36&&a[j]<=61)
- System.out.print((char)(a[j]-36+'a'));
- }
-
- System.out.println();
- if(T!=0)
- System.out.println();
- }
- }
-
- private static int v(char c)
- {
- if(c>='0'&&c<='9')
- return c-'0';
- else if(c>='A'&&c<='Z')
- return c-'A'+10;
- else if(c>='a'&&c<='z')
- return c-'a'+36;
- return 0;
- }
- }
复制代码 程序测试:
该程序循环的次数:
3
输入数的进制:
16
要转换的进制:
10
16要转换的10进制的数:
3A0C92075C0DBF3B8ACBC5F96CE3F0AD23A0C92075C0DBF3B8ACBC5F96CE3F0AD23A0C92075C0DBF3B8ACBC5F96CE3F0AD23A0C92075C0DBF3B8ACBC5F96CE3F0AD23A0C92075C0DBF3B8ACBC5F96CE3F0AD23A0C92075C0DBF3B8ACBC5F96CE3F0AD23A0C92075C0DBF3B8ACBC5F96CE3F0AD23A0C92075C0DBF3B8ACBC5F96CE3F0AD23A0C92075C0DBF3B8ACBC5F96CE3F0AD23A0C92075C0DBF3B8ACBC5F96CE3F0AD2
16进制要转换10进制的数:3A0C92075C0DBF3B8ACBC5F96CE3F0AD23A0C92075C0DBF3B8ACBC5F96CE3F0AD23A0C92075C0DBF3B8ACBC5F96CE3F0AD23A0C92075C0DBF3B8ACBC5F96CE3F0AD23A0C92075C0DBF3B8ACBC5F96CE3F0AD23A0C92075C0DBF3B8ACBC5F96CE3F0AD23A0C92075C0DBF3B8ACBC5F96CE3F0AD23A0C92075C0DBF3B8ACBC5F96CE3F0AD23A0C92075C0DBF3B8ACBC5F96CE3F0AD23A0C92075C0DBF3B8ACBC5F96CE3F0AD2
16进制要转换10进制的数的结果:5189790491602597986321792601378459415876170151934523553765647853292812302618388570027833028128452303365432624187530134200724035918282659464828026132771719550795629865585330708151205798437918035656733637605072814608684326014531595556155964034625688497671791861255010744071740982754706824878485680965537885138716630442361801903575157820061983533339500618948754726522672572296227958719314050905213650
输入数的进制:
10
要转换的进制:
16
10要转换的16进制的数:
1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890
10进制要转换16进制的数:1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890
10进制要转换16进制的数的结果:8B4A290236025209EDB94DC1478DA0DCC39E1B4D1B546E5961188994FAEA6931E04E0378629248B7282A5E6064A2AE80A67A83DC209F5BB42058208D441AA019FE2F4DB9E90CBCD6CE0045AD5139317F3ED5E5E7EB001B51E3B725A501C2A01788A9935E243D1161EF7BF14BACCFF196CE3F0AD2
输入数的进制:
16
要转换的进制:
8
16要转换的8进制的数:
3A0C92075C0DBF3B8ACBC5F96CE3F0AD23A0C92075C0DBF3B8ACBC5F96CE3F0AD23A0C92075C0DBF3B8ACBC5F96CE3F0AD23A0C92075C0DBF3B8ACBC5F96CE3F0AD23A0C92075C0DBF3B8ACBC5F96CE3F0AD23A0C92075C0DBF3B8ACBC5F96CE3F0AD2
16进制要转换8进制的数:3A0C92075C0DBF3B8ACBC5F96CE3F0AD23A0C92075C0DBF3B8ACBC5F96CE3F0AD23A0C92075C0DBF3B8ACBC5F96CE3F0AD23A0C92075C0DBF3B8ACBC5F96CE3F0AD23A0C92075C0DBF3B8ACBC5F96CE3F0AD23A0C92075C0DBF3B8ACBC5F96CE3F0AD2
16进制要转换8进制的数的结果:164062220165601557635612627427713316176053221640622201656015576356126274277133161760532216406222016560155763561262742771331617605322164062220165601557635612627427713316176053221640622201656015576356126274277133161760532216406222016560155763561262742771331617605322
希望能帮到你!!!
|