Java如何实现10进制转任意进制

说明:其实jdk提供了10进制转2、4、8、10、16、32这几个进制的方法。但是很不幸的是,在网上找了很多资料,对于如何转为其他进制的博文很多都是扯淡。后来看到了老徐写的一个公共类,然后对这个公共类进行了整理。
在这里插入图片描述

实例代码如下所示:(如果需要自己改造,则根据规律来处理即可。)

package com.sowell.aaa.controller.test3;

public class NumericConvertUtils
{
    /**
     * 在进制表示中的字符集合,0-Z分别用于表示最大为62进制的符号表示
     */
    private static final char[] digits = {'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 char[] digits26 = {'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 char[] digits32 = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A',
        'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'V', 'W',
        'X', 'Y', 'Z'};

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

    public static String toOtherNumberSystem(long number, int seed)
    {
        if (number < 0)
        {
            number = ((long)2 * 0x7fffffff) + number + 2;
        }
        char[] buf = new char[32];
        int charPos = 32;
        while ((number / seed) > 0)
        {
            buf[--charPos] = digits[(int)(number % seed)];
            number /= seed;
        }
        buf[--charPos] = digits[(int)(number % seed)];
        return new String(buf, charPos, (32 - charPos));
    }

    public static long toDecimalNumber(String number, int seed)
    {
        char[] charBuf = number.toCharArray();
        if (seed == 10)
        {
            return Long.parseLong(number);
        }

        long result = 0, base = 1;

        for (int i = charBuf.length - 1; i >= 0; i-- )
        {
            int index = 0;
            for (int j = 0, length = digits.length; j < length; j++ )
            {
                // 找到对应字符的下标,对应的下标才是具体的数值
                if (digits[j] == charBuf[i])
                {
                    index = j;
                }
            }
            result += index * base;
            base *= seed;
        }
        return result;
    }

    public static String toOtherNumberSystem26(long number, int seed)
    {
        if (number < 0)
        {
            number = ((long)2 * 0x7fffffff) + number + 2;
        }
        char[] buf = new char[32];
        int charPos = 32;
        while ((number / seed) > 0)
        {
            buf[--charPos] = digits26[(int)(number % seed)];
            number /= seed;
        }
        buf[--charPos] = digits26[(int)(number % seed)];
        return new String(buf, charPos, (32 - charPos));
    }

    public static long toDecimalNumber26(String number, int seed)
    {
        char[] charBuf = number.toCharArray();
        if (seed == 10)
        {
            return Long.parseLong(number);
        }

        long result = 0, base = 1;

        for (int i = charBuf.length - 1; i >= 0; i-- )
        {
            int index = 0;
            for (int j = 0, length = digits26.length; j < length; j++ )
            {
                // 找到对应字符的下标,对应的下标才是具体的数值
                if (digits26[j] == charBuf[i])
                {
                    index = j;
                }
            }
            result += index * base;
            base *= seed;
        }
        return result;
    }

    public static String toOtherNumberSystem52(long number, int seed)
    {
        if (number < 0)
        {
            number = ((long)2 * 0x7fffffff) + number + 2;
        }
        char[] buf = new char[32];
        int charPos = 32;
        while ((number / seed) > 0)
        {
            buf[--charPos] = digits52[(int)(number % seed)];
            number /= seed;
        }
        buf[--charPos] = digits52[(int)(number % seed)];
        return new String(buf, charPos, (32 - charPos));
    }

    public static long toDecimalNumber52(String number, int seed)
    {
        char[] charBuf = number.toCharArray();
        if (seed == 10)
        {
            return Long.parseLong(number);
        }

        long result = 0, base = 1;

        for (int i = charBuf.length - 1; i >= 0; i-- )
        {
            int index = 0;
            for (int j = 0, length = digits52.length; j < length; j++ )
            {
                // 找到对应字符的下标,对应的下标才是具体的数值
                if (digits52[j] == charBuf[i])
                {
                    index = j;
                }
            }
            result += index * base;
            base *= seed;
        }
        return result;
    }

    public static String toOtherNumberSystem58(long number, int seed)
    {
        if (number < 0)
        {
            number = ((long)2 * 0x7fffffff) + number + 2;
        }
        char[] buf = new char[32];
        int charPos = 32;
        while ((number / seed) > 0)
        {
            buf[--charPos] = digits58[(int)(number % seed)];
            number /= seed;
        }
        buf[--charPos] = digits58[(int)(number % seed)];
        return new String(buf, charPos, (32 - charPos));
    }

    public static long toDecimalNumber58(String number, int seed)
    {
        char[] charBuf = number.toCharArray();
        if (seed == 10)
        {
            return Long.parseLong(number);
        }

        long result = 0, base = 1;

        for (int i = charBuf.length - 1; i >= 0; i-- )
        {
            int index = 0;
            for (int j = 0, length = digits58.length; j < length; j++ )
            {
                // 找到对应字符的下标,对应的下标才是具体的数值
                if (digits58[j] == charBuf[i])
                {
                    index = j;
                }
            }
            result += index * base;
            base *= seed;
        }
        return result;
    }

    public static String toOtherNumberSystem32(long number, int seed)
    {
        if (number < 0)
        {
            number = ((long)2 * 0x7fffffff) + number + 2;
        }
        char[] buf = new char[32];
        int charPos = 32;
        while ((number / seed) > 0)
        {
            buf[--charPos] = digits32[(int)(number % seed)];
            number /= seed;
        }
        buf[--charPos] = digits32[(int)(number % seed)];
        return new String(buf, charPos, (32 - charPos));
    }

    public static long toDecimalNumber32(String number, int seed)
    {
        char[] charBuf = number.toCharArray();
        if (seed == 10)
        {
            return Long.parseLong(number);
        }

        long result = 0, base = 1;

        for (int i = charBuf.length - 1; i >= 0; i-- )
        {
            int index = 0;
            for (int j = 0, length = digits32.length; j < length; j++ )
            {
                // 找到对应字符的下标,对应的下标才是具体的数值
                if (digits32[j] == charBuf[i])
                {
                    index = j;
                }
            }
            result += index * base;
            base *= seed;
        }
        return result;
    }

    public static void main(String[] args)
    {
        long time = 1000000001111L;
        System.out.println("16进制结果" + Long.toUnsignedString(time, 16));
        System.out.println("32进制结果" + toOtherNumberSystem32(time, 32));
        System.out.println("36进制结果" + Long.toUnsignedString(time, 36));
        System.out.println("26进制结果" + toOtherNumberSystem26(time, 26));
        System.out.println("52进制结果" + toOtherNumberSystem52(time, 52));
        System.out.println("58进制结果" + toOtherNumberSystem58(time, 58));

    }
}

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
### 回答1: Java 中可以使用 Integer 类的 toString(int num, int radix) 方法来实现任意进制换。该方法的第一个参数为需要换的十进制数字,第二个参数为换后的进制。 例如,将十进制数字 10 换为二进制: ``` String binary = Integer.toString(10, 2); System.out.println(binary); // 1010 ``` 或者使用Integer.toHexString(int i) 和Integer.toOctalString(int i) 将十进制数字 10 换为十六进制 ``` String hex = Integer.toHexString(10); System.out.println(hex); // a ``` 将十进制数字 10 换为八进制 ``` String oct = Integer.toOctalString(10); System.out.println(oct); // 12 ``` ### 回答2: Java 实现任意进制换可以通过以下步骤实现: 1. 定义一个方法,传入三个参数:待换的数值(作为字符串)、原始进制数、目标进制数。 2. 将字符串形式的数值换为十进制数。可以通过使用 Integer 类的 parseInt() 方法将字符串换为整数。 3. 将十进制换为目标进制。可以使用 Integer 类的 toString() 方法,传入待换的十进制数和目标进制数,将其换为指定进制的字符串形式。 4. 返回换后的结果。 以下是一个示例代码: ``` public class NumberSystemConverter { public static void main(String[] args) { String number = "101"; int sourceBase = 2; int targetBase = 10; String result = convertNumberSystem(number, sourceBase, targetBase); System.out.println(result); } public static String convertNumberSystem(String number, int sourceBase, int targetBase) { int decimalNumber = Integer.parseInt(number, sourceBase); String result = Integer.toString(decimalNumber, targetBase); return result; } } ``` 在示例代码中,将二进制数 "101" 换为十进制数,并打印出结果 "5"。你可以根据需要修改待换的数值和进制数,以及打印方式。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值