JavaScript 进制转换 All In One
JavaScript 进制转换 All In One
js base conversion
native solutions
十进制
转二进制
Number.prototype.toString()
将十
进制数子转换为二
进制、八
进制、十六进
制 等进制的字符串
(5).toString(2);
// '101'
let tTob = (5).toString(2);
console.log(tTob, typeof tTob);
// 101 string
(5).toString(2);
// '101'
(5.5).toString(2);
// '101.1'
(5.6).toString(2);
// '101.1001100110011001100110011001100110011001100110011'
radix
基数
radix Optional
An integer in the range 2
through 36
specifying the base
to use for representing the number value.
Defaults to 10
.
toString()
toString(radix)
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toString
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/toString
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/toString
二进制
转十进制
parseInt('101', 2);
// 5
let bTot = parseInt('101', 2);
console.log(bTot, typeof bTot);
// 5 'number'
radix
基数
radix Optional
An integer between 2
and 36
that represents the radix (the base
in mathematical numeral systems) of the string.
It is converted to a 32-bit integer;
if it's nonzero and outside the range of [2, 36] after conversion, the function will always return NaN
.
If 0 or not provided, the radix will be inferred based on string's value.
Be careful — this does not
always default to 10
!
The description below explains in more detail what happens when radix is not provided.
parseInt(string)
parseInt(string, radix)
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseInt
pure js solutions
// js 算法实现
demos
function countBits(n: number): number[] {
let temp = [];
for(let i = 0; i <= n; i ++) {
// js 二进制 转换
let b = [...tTob(i)].filter(i => i === `1`).length;
temp.push(b);
}
return temp;
};
function tTob (t: number): string {
return t.toString(2);
}
https://leetcode.com/problems/counting-bits/description/?envType=study-plan-v2&envId=leetcode-75
// function tTob (t: number): string {
// let i = 0;
// let str = 0;
// if(t % 2 !== 0) {
// str += 1;
// } else {
// str += 0;
// }
// while(2**i < t) {
// i += 1;
// str += 10**i;
// // if(i % 2 !== 0) {
// // str += 10**i;
// // }
// }
// // while(t / 2) {
// // t = Math.floor(t / 2);
// // i += 1;
// // str += 10**i;
// // }
// console.log(`str =`, str);
// return `${str}`;
// }
// function tTob (t: number): string {
// let n = 0;
// while(2**n < t) {
// n += 1;
// }
// console.log(`n =`, n);
// let str = 0;
// if(t % 2 !== 0) {
// str += 1;
// } else {
// str += 0;
// }
// let sum = str;
// for(let i = 1; i <= n; i++) {
// if(sum + 2**i < t) {
// str += 10**i;
// sum += 2**i;
// }
// }
// console.log(`str =`, str);
// return `${str}`;
// }
// function tTob (t: number): str {
// let str = ``;
// let n = 0;
// while(2**n < t) {
// n += 1;
// }
// console.log(`n =`, n);
// let sum = 0;
// for(let i = 0; i < n; i++) {
// sum += 2**i;
// if(sum < t) {
// str += i
// }
// }
// return str;
// // return parseInt(str, 10);
// // return parseInt(t, 2);
// }
(🐞 反爬虫测试!打击盗版⚠️)如果你看到这个信息, 说明这是一篇剽窃的文章,请访问 https://www.cnblogs.com/xgqfrms/ 查看原创文章!
JavaScript 进制转换
LeetCode 171. Excel Sheet Column Number / Excel 工作表列号
function titleToNumber(columnTitle: string): number {
// 如何动态生成 字典 ✅ 26 进制
// A-Z -> 1-26
const strs = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
const obj = {};
for(let i = 0; i < strs.length; i++) {
obj[strs[i]] = i + 1;
}
let result = 0
const arr = columnTitle.split('')
let n = arr.length;
for(let i = 0; i < n; i++) {
let num = obj[arr[i]];
result += num * 26 ** (n - i - 1);
}
return result
};
/*
str = `A`
str.charAt(0)
// 65
str.charCodeAt(0)
// 65
str.codePointAt(0)
// 65
*/
https://leetcode.com/problems/excel-sheet-column-number/
https://www.cnblogs.com/xgqfrms/p/17650033.html
refs
https://www.cnblogs.com/jimojianghu/p/15624693.html
https://www.mlxz.top/index.php/archives/69/
©xgqfrms 2012-2021
www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!
原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!
本文首发于博客园,作者:xgqfrms,原文链接:https://www.cnblogs.com/xgqfrms/p/18216913
未经授权禁止转载,违者必究!