Java基础知识——数组实现进制转化

class ArrayTrans
{
	public static void main(String[] args) 
	{
		System.out.println(" 十进制-->二进制:55");
		toBin(55);
		System.out.println();
		System.out.println(" 十进制-->八进制:55");
		toOct(55);
		System.out.println();
		System.out.println(" 十进制-->十六进制:55");
		toHex(55);
	}

	/*
     十进制-->二进制
     */
	 public static void toBin(int num)
	{
		trans(num,1,1);
	}
     
	 /*
	 十进制-->八进制
	 */
	 public static void toOct(int num)
	{
		trans(num,7,3);
	}

	/*
	十进制-->十六进制
	*/
	public static void toHex(int num)
	{
		trans(num,15,4);
	}
	

	public static void trans(int num,int base,int offset)
	{
		char [] chs = { '0','1','2','3',
						'4','5','6','7',
						'8','9','A','B',
						'C','D','E','F'};
		char [] arr = new char[32];
		int pos = arr.length;

		while(num != 0)
		{
			int temp = num & base;
			arr[--pos] = chs[temp];
			num = num >>> offset;
		}

		for(int x = pos;x 

 

你可能感兴趣的:(【Java】)