10进制转任意进制,任意进制转10进制

系统需要实现任意10进制和任意进制之间的转化(36进制以内),任意进制转化为10进制 10进制转换为任意进制

package com.myhexin.bootdemo.level3;

import java.util.Stack;

/**
 * 进制数字转化
 *
 * @author qiaolei
 * @date 2022/09/16
 */
public class SwitchNumber {
    public static void main(String[] args) {
        int src = 100;
        int toBase = 36;
        String outPutStr = "";
        outPutStr = Base10ToBaseN(src, toBase, outPutStr);
        ;

        System.out.println(outPutStr);
        int outPutStr2=0;
        outPutStr2=BaseNtoBase10("1A1",36);
        System.out.println(outPutStr2);
    }

    public static String Base10ToBaseN(int src, int toBase, String outPutStr) {
        String chs = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        StringBuffer sb = new StringBuffer();
        String digths = chs.substring(0, toBase);
        Stack s = new Stack<>();
        while (src != 0) {
            s.push(digths.charAt(src % toBase));
            src /= toBase;
        }
        while (!s.isEmpty()) {

            sb.append(s.pop());

        }
        outPutStr = sb.toString();
        return outPutStr;
    }

    /**
     * n进制转化为10进制
     *
     * @param planstr n 进制字符串
     * @return iBase 表示几进制
     */

    public static int BaseNtoBase10(String planstr, int iBase) {
        int outPutStr = 0;
        int temp = 0;
        int d;
        int p = 0;
        char c;
        int length = planstr.length();
        for (int i = 0; i < length; i++) {
            c = planstr.charAt(i);
            //将数字转化为对应的数字
            if (c >= 'A' && c <= 'Z') {
                d = c - 55;
            } else if (c >= 'a' && c <= 'Z') {
                d = c - 87;
            } else {
                d = c - 48;
            }

            //当前位权

            p = length - 1 - i;
            //0^0=1
            if (d != 0) {
                temp += d * (int) Math.pow(iBase, p);
            }
        }
        outPutStr = temp;
        return outPutStr;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是使用C语言编写十进制任意进制的函数代码: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> char* dec2base(int num, int base) { char* res = (char*) malloc(sizeof(char) * 32); int i = 0; while (num > 0) { int rem = num % base; res[i] = (rem > 9) ? (rem - 10 + 'A') : (rem + '0'); i++; num /= base; } if (i == 0) { res[i] = '0'; i++; } res[i] = '\0'; strrev(res); return res; } int main() { int num, base; char* res; printf("Enter a decimal number: "); scanf("%d", &num); printf("Enter the base to convert to: "); scanf("%d", &base); res = dec2base(num, base); printf("%d in base %d is %s\n", num, base, res); free(res); // remember to free the memory allocated by malloc() return 0; } ``` 解释: - `dec2base` 函数接受两个参数:一个十进制整数 `num` 和一个目标进制 `base`,返回一个字符串,表示 `num` 在目标进制下的数值。 - 在函数内部,使用循环将 `num` 换成目标进制的数值,并保存到一个字符串中。 - 字符串的每个字符表示目标进制下的一位数值,当余数大于 9 时,使用字母 A 到 Z 表示。因此在换的过程中需要判断余数的值。 - 为了得到正确的结果,换后的字符串需要反一下,因为我们是从低位到高位换的。 - 在主函数中,读取用户输入的十进制数和目标进制,并调用 `dec2base` 函数来进行换。最后输出结果,并释放动态分配的内存。 注意:此程序只支持换到进制 2 到 36 之间的数值。如果目标进制超过了这个范围,程序会出错。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值