java算法----进制之间的转换

package com.zhenlvwang.interview;

import java.util.Stack;

/**
 * 将十进制转换为八进制
 * @author yangjianzhou
 *
 */
public class Problem1 {
	
	public static void main(String[] args) {
		Problem1 p = new Problem1();
		System.out.println(p.decimalToOctal(64));
		System.out.println(Integer.toHexString(Integer.parseInt("1111", 2)));//运用java的API实现2进制到16进制的转换
		System.out.println(Integer.toOctalString(10));//十进制到八进制
	}
	
	@SuppressWarnings("unchecked")
	public String decimalToOctal(int a){
		
		int quotient = 0;
		int remainder = 0;
		Stack stack = new Stack();
		String result = "";
		while(a>0){
			quotient = a/8;
			remainder = a%8;
			stack.push(remainder);
			a = quotient;
            
		}
		while(!stack.isEmpty()){
			result = result +stack.pop();
		}
		return result;
	}
	
	

}



运行结果:
100
f
12

你可能感兴趣的:(java算法)