canna-cloud【七】64进制以内任意进制的转化

版权声明: https://blog.csdn.net/jiangxuexuanshuang/article/details/89003985

项目地址:https://github.com/xuanshuangchen/canna-cloud.git

1、10进制转指定进制

2、指定进制转10进制

3、进制的最大值

4、进制的枚举值,方便操作

进制转化类

public class Radix {

    private final static char[] DIGITS_BASE = {
            '0' , '1' , '2' , '3' , '4' , '5' ,
            '6' , '7' , '8' , '9' , 'A' , 'B' ,
            'C' , 'D' , 'E' , 'F' , 'G' , 'H' ,
            'I' , 'J' , 'K' , 'L' , 'M' , 'N' ,
            'O' , 'P' , 'Q' , 'R' , 'S' , 'T' ,
            'U' , 'V' , 'W' , 'X' , 'Y' , 'Z' ,
            'a' , 'b' , 'c' , 'd' , 'e' , 'f' ,
            'g' , 'h' , 'i' , 'j' , 'k' , 'l' ,
            'm' , 'n' , 'o' , 'p' , 'q' , 'r' ,
            's' , 't' , 'u' , 'v' , 'w' , 'x' ,
            'y' , 'z' , '[' , ']'
    };

    private static final int ASCII_SIZE = 128;

    private static final int[] DIGITS_INDEX = new int[ASCII_SIZE];

	private static final char BLANK_CHAR = '0';

	/**
	 * 进制数
	 */
	@Getter
	protected int radix;

	/**
	 * 空白补充字符
	 */
	@Getter
	protected char blankChar;

    public static final int MAX_RADIX = 64;

    public static final int MIN_RADIX = 2;
	
	/**
	 * 进制最大的字符
	 */
	@Getter
	protected char maxChar;

	private char[] digits;

    static {
        for (int i = 0; i < ASCII_SIZE; i++) {
            DIGITS_INDEX[i] = -1;
        }
    }

    public Radix(int radix){
        this(radix, DIGITS_BASE);
    }

    public Radix(int radix, int beginIndex, int endIndex){
        char radixDigit[] = new char[endIndex - beginIndex];
        for (int i = beginIndex; i < endIndex; i++) {
            radixDigit[i - beginIndex] = DIGITS_BASE[i];
        }
        this.init(radix, radixDigit);
    }

    public Radix(int radix, char[] digits){
        this.init(radix, digits);
    }

	private void init(int radix, char[] digits){
        if (radix < MIN_RADIX) {
            throw new NumberFormatException("radix " + radix + " less than MIN_RADIX");
        }
        if (radix > MAX_RADIX) {
            throw new NumberFormatException("radix " + radix + " greater than MAX_RADIX");
        }

        this.digits = digits;

		this.radix = radix;

        this.blankChar = BLANK_CHAR;

        this.maxChar = digits[radix - 1];

        for (int i = 0; i < digits.length; i++) {
            DIGITS_INDEX[digits[i]] = i;
        }
	}

	public String toRadixWithBlank(long value, long length) {
		String dest = toRadix(value);
		if(dest.length() > length){
            throw new StringIndexOutOfBoundsException("string max than setting length");
		}
		if (dest.length() < length) {
			dest = this.getRepeatStr(blankChar, length - dest.length()) + dest;
		}

		return dest;
	}

	public long toDecimal(String value) {
		return parseLong(value, radix);
	}
	
	public long getMaxValue(long bit) {
		String value = this.getRepeatStr(maxChar, bit);
		return parseLong(value, radix);
	}
	
	private String getRepeatStr(char value, long length){
		StringBuilder builder = new StringBuilder();
		for(int i = 0; i < length; i ++){
			builder.append(value);
		}
		return builder.toString();
	}

    public String toRadix(long i) {
        if (radix == 10)
            return Long.toString(i);
        char[] buf = new char[65];
        int charPos = 64;
        boolean negative = (i < 0);

        if (!negative) {
            i = -i;
        }

        while (i <= -radix) {
            buf[charPos--] = digits[(int)(-(i % radix))];
            i = i / radix;
        }
        buf[charPos] = digits[(int)(-i)];

        if (negative) {
            buf[--charPos] = '-';
        }

        return new String(buf, charPos, (65 - charPos));
    }

    private long parseLong(String s, int radix) throws NumberFormatException
    {
        if (s == null) {
            throw new NumberFormatException("null");
        }

        if (radix < MIN_RADIX) {
            throw new NumberFormatException("radix " + radix + " less than MIN_RADIX");
        }
        if (radix > MAX_RADIX) {
            throw new NumberFormatException("radix " + radix + " greater than MAX_RADIX");
        }

        long result = 0;
        boolean negative = false;
        int i = 0, len = s.length();
        long limit = -Long.MAX_VALUE;
        long multmin;
        int digit;

        if (len > 0) {
            char firstChar = s.charAt(0);
            if (firstChar < '0') { // Possible leading "+" or "-"
                if (firstChar == '-') {
                    negative = true;
                    limit = Long.MIN_VALUE;
                } else if (firstChar != '+')
                    throw new NumberFormatException("For input string: \"" + s + "\"");

                if (len == 1) // Cannot have lone "+" or "-"
                    throw new NumberFormatException("For input string: \"" + s + "\"");
                i++;
            }
            multmin = limit / radix;
            while (i < len) {
                // Accumulating negatively avoids surprises near MAX_VALUE
                digit = DIGITS_INDEX[s.charAt(i++)];
                if (digit < 0) {
                    throw new NumberFormatException("For input string: \"" + s + "\"");
                }
                if (digit >= radix) {
                    throw new NumberFormatException("For input string: \"" + s + "\"");
                }
                if (result < multmin) {
                    throw new NumberFormatException("For input string: \"" + s + "\"");
                }
                result *= radix;
                if (result < limit + digit) {
                    throw new NumberFormatException("For input string: \"" + s + "\"");
                }
                result -= digit;
            }
        } else {
            throw new NumberFormatException("For input string: \"" + s + "\"");
        }
        return negative ? result : -result;
    }

}

进制枚举类:

public enum RadixEnum {
    Radix2(2),
    Radix3(3),
    Radix4(4),
    Radix5(5),
    Radix6(6),
    Radix7(7),
    Radix8(8),
    Radix9(9),
    Radix10(10),
    Radix11(11),
    Radix12(12),
    Radix13(13),
    Radix14(14),
    Radix15(15),
    Radix16(16),
    Radix17(17),
    Radix18(18),
    Radix19(19),
    Radix20(20),
    Radix21(21),
    Radix22(22),
    Radix23(23),
    Radix24(24),
    Radix25(25),
    Radix26(26),
    Radix27(27),
    Radix28(28),
    Radix29(29),
    Radix30(30),
    Radix31(31),
    Radix32(32),
    Radix33(33),
    Radix34(34),
    Radix35(35),
    Radix36(36),
    Radix37(37),
    Radix38(38),
    Radix39(39),
    Radix40(40),
    Radix41(41),
    Radix42(42),
    Radix43(43),
    Radix44(44),
    Radix45(45),
    Radix46(46),
    Radix47(47),
    Radix48(48),
    Radix49(49),
    Radix50(50),
    Radix51(51),
    Radix52(52),
    Radix53(53),
    Radix54(54),
    Radix55(55),
    Radix56(56),
    Radix57(57),
    Radix58(58),
    Radix59(59),
    Radix60(60),
    Radix61(61),
    Radix62(62),
    Radix63(63),
    Radix64(64)
    ;

    public static final ImmutableMap<Integer, RadixEnum> radixEnumMap = ImmutableMap.<Integer, RadixEnum>
            of(2, Radix2)
            .of(3, Radix3)
            .of(4, Radix4)
            .of(5, Radix5)
            .of(6, Radix6)
            .of(7, Radix7)
            .of(8, Radix8)
            .of(9, Radix9)
            .of(10, Radix10)
            .of(11, Radix11)
            .of(12, Radix12)
            .of(13, Radix13)
            .of(14, Radix14)
            .of(15, Radix15)
            .of(16, Radix16)
            .of(17, Radix17)
            .of(18, Radix18)
            .of(19, Radix19)
            .of(20, Radix20)
            .of(21, Radix21)
            .of(22, Radix22)
            .of(23, Radix23)
            .of(24, Radix24)
            .of(25, Radix25)
            .of(26, Radix26)
            .of(27, Radix27)
            .of(28, Radix28)
            .of(29, Radix29)
            .of(30, Radix30)
            .of(31, Radix31)
            .of(32, Radix32)
            .of(33, Radix33)
            .of(34, Radix34)
            .of(35, Radix35)
            .of(36, Radix36)
            .of(37, Radix37)
            .of(38, Radix38)
            .of(39, Radix39)
            .of(40, Radix40)
            .of(41, Radix41)
            .of(42, Radix42)
            .of(43, Radix43)
            .of(44, Radix44)
            .of(45, Radix45)
            .of(46, Radix46)
            .of(47, Radix47)
            .of(48, Radix48)
            .of(49, Radix49)
            .of(50, Radix50)
            .of(51, Radix51)
            .of(52, Radix52)
            .of(53, Radix53)
            .of(54, Radix54)
            .of(55, Radix55)
            .of(56, Radix56)
            .of(57, Radix57)
            .of(58, Radix58)
            .of(59, Radix59)
            .of(60, Radix60)
            .of(61, Radix61)
            .of(62, Radix62)
            .of(63, Radix63)
            .of(64, Radix64);

    private Radix radix;

    private RadixEnum(int radix) {
        this.radix = new Radix(radix);
    }

    public String toRadixWithBlank(long value, long length) {
        return this.radix.toRadixWithBlank(value, length);
    }

    public long toDecimal(String value) {
        return this.radix.toDecimal(value);
    }

    public long getMaxValue(long bit) {
        return this.radix.getMaxValue(bit);
    }

    public String toRadix(long i) {
        return this.radix.toRadix(i);
    }

    public RadixEnum valueOf(int radix) {
        if (radix < Radix.MIN_RADIX) {
            throw new NumberFormatException("radix " + radix + " less than MIN_RADIX");
        }
        if (radix > Radix.MAX_RADIX) {
            throw new NumberFormatException("radix " + radix + " greater than MAX_RADIX");
        }
        return radixEnumMap.get(radix);
    }
}

猜你喜欢

转载自blog.csdn.net/jiangxuexuanshuang/article/details/89003985