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/
https://stackoverflow.com/questions/1337419/how-do-you-convert-numbers-between-different-bases-in-javascript
©xgqfrms 2012-2021
www.cnblogs.com/xgqfrms 釋出文章使用:只允許註冊使用者才可以訪問!
原創文章,版權所有©️xgqfrms, 禁止轉載 🈲️,侵權必究⚠️!