笔试题:将输入的数字转为成四进制表示。

package com.spring.demo.controller;


/**
 * @author wxe
 * @since 1.0.0
 */
public class FourTest {

    public static void main(String[] args) {
        
        System.out.println(getFourHex(520));
    }

    final static char[] digits = { '0', '1', '2', '3' };// 四进制表示数0,1,2,3

    public static String getFourHex(int origin) {
        char[] result = new char[32];
        int charPostion = 32;
        
        while ((origin / 4) > 0) {
            result[--charPostion] = digits[origin % 4];
            origin = origin / 4;
        }
        
        result[--charPostion] = digits[origin % 4];
        return new String(result,charPostion,32-charPostion);
    }

}

测试:520    对应的四进制为:20020

 

 posted on 2017-08-30 10:53  abysstoabyss  阅读(120)  评论(0)    收藏  举报