编程习题3

输入一个十六进制,将其转换为三进制

public class Test10 {
    public static void main(String[] args) {
        String num = "A";
        StringBuffer result = new StringBuffer("");
        long n = 0;
        long k = 1;
        for(int i = num.length() -1; i >= 0;i--){
            int ch = num.charAt(i);
            if(ch >= '0' && ch <='9'){
                ch -= '0';
            }else if(ch >= 'A' && ch <= 'F'){
                ch -= ('A'-10);
            }else
                return;
            n +=(ch*k);
            k *= 16;
        }
        for(;n != 0; n /=3){
            result.append(n%3);
        }
        result = result.reverse();
        System.out.println(result);
    }
}
原文地址:https://www.cnblogs.com/YESheng/p/3657618.html