在JavaScript中从一个基数转换为另一个基数 [英] Convert from one base to another in JavaScript

查看:247
本文介绍了在JavaScript中从一个基数转换为另一个基数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在JavaScript中,有没有内置函数将整数从一个给定的基数转换为另一个给定的基数?我注意到已经可以使用 toString(numberToConvertTo)将十进制数转换为另一个基数,但我还没有找到可以转换的通用函数从任何基地到任何其他基地,如图所示:

In JavaScript, is there any built-in function for converting an integer from one given base to another given base? I've noticed that it's already possible to convert a decimal number to another base using toString(numberToConvertTo), but I haven't yet found a general-purpose function that can convert from any base to any other base, as shown:

convertFrom(toConvert, baseToConvertFrom, baseToConvertTo){
    //convert the number from baseToConvertFrom to BaseToConvertTo
}


推荐答案

调用 parseInt(str,fromBase)转换为基数10(或更确切地说,转换为实际数字),然后调用 num.toString(toBase)

Call parseInt(str, fromBase) to convert to base 10 (or rather, to an actual number), then call num.toString(toBase).

这篇关于在JavaScript中从一个基数转换为另一个基数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆