欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

javascript實(shí)現(xiàn)二進(jìn)制、十進(jìn)制、十六進(jìn)制和八進(jìn)制之間相互轉(zhuǎn)換的方法

 更新時(shí)間:2025年02月25日 08:29:35   作者:桃子叔叔  
JavaScript提供了多種方法進(jìn)行不同進(jìn)制之間的轉(zhuǎn)換,包括十進(jìn)制轉(zhuǎn)二進(jìn)制、八進(jìn)制、十六進(jìn)制,以及二進(jìn)制、八進(jìn)制、十六進(jìn)制之間的相互轉(zhuǎn)換,通過使用Number.prototype.toString()和parseInt()函數(shù),可以實(shí)現(xiàn)這些轉(zhuǎn)換,需要的朋友可以參考下

在 JavaScript 中,可以方便地進(jìn)行二進(jìn)制、十進(jìn)制、十六進(jìn)制和八進(jìn)制之間的相互轉(zhuǎn)換。下面為你詳細(xì)介紹每種轉(zhuǎn)換的代碼示例和原理。

1. 十進(jìn)制轉(zhuǎn)其他進(jìn)制

十進(jìn)制轉(zhuǎn)二進(jìn)制

使用 Number.prototype.toString(2) 方法將十進(jìn)制數(shù)轉(zhuǎn)換為二進(jìn)制字符串。

const decimalNumber = 10;
const binaryString = decimalNumber.toString(2);
console.log(binaryString); // 輸出: '1010'

講解toString() 方法是 JavaScript 中 Number 對(duì)象的一個(gè)方法,它接受一個(gè)可選參數(shù) radix,表示轉(zhuǎn)換的進(jìn)制,取值范圍是 2 到 36。當(dāng) radix 為 2 時(shí),就會(huì)將十進(jìn)制數(shù)轉(zhuǎn)換為二進(jìn)制字符串。

十進(jìn)制轉(zhuǎn)八進(jìn)制

使用 Number.prototype.toString(8) 方法將十進(jìn)制數(shù)轉(zhuǎn)換為八進(jìn)制字符串。

const decimalNumber = 10;
const octalString = decimalNumber.toString(8);
console.log(octalString); // 輸出: '12'

講解:同樣,當(dāng) radix 為 8 時(shí),toString() 方法會(huì)將十進(jìn)制數(shù)轉(zhuǎn)換為八進(jìn)制字符串。

十進(jìn)制轉(zhuǎn)十六進(jìn)制

使用 Number.prototype.toString(16) 方法將十進(jìn)制數(shù)轉(zhuǎn)換為十六進(jìn)制字符串。

const decimalNumber = 255;
const hexadecimalString = decimalNumber.toString(16);
console.log(hexadecimalString); // 輸出: 'ff'

講解:當(dāng) radix 為 16 時(shí),toString() 方法會(huì)將十進(jìn)制數(shù)轉(zhuǎn)換為十六進(jìn)制字符串,其中 10 - 15 會(huì)用字母 a - f 表示。

2. 其他進(jìn)制轉(zhuǎn)十進(jìn)制

二進(jìn)制轉(zhuǎn)十進(jìn)制

使用 parseInt() 函數(shù),將二進(jìn)制字符串轉(zhuǎn)換為十進(jìn)制數(shù)。

const binaryString = '1010';
const decimalNumber = parseInt(binaryString, 2);
console.log(decimalNumber); // 輸出: 10

講解parseInt() 函數(shù)接受兩個(gè)參數(shù),第一個(gè)參數(shù)是要轉(zhuǎn)換的字符串,第二個(gè)參數(shù)是字符串的進(jìn)制。當(dāng)?shù)诙€(gè)參數(shù)為 2 時(shí),會(huì)將二進(jìn)制字符串轉(zhuǎn)換為十進(jìn)制數(shù)。

八進(jìn)制轉(zhuǎn)十進(jìn)制

使用 parseInt() 函數(shù),將八進(jìn)制字符串轉(zhuǎn)換為十進(jìn)制數(shù)。

const octalString = '12';
const decimalNumber = parseInt(octalString, 8);
console.log(decimalNumber); // 輸出: 10

講解:當(dāng) parseInt() 函數(shù)的第二個(gè)參數(shù)為 8 時(shí),會(huì)將八進(jìn)制字符串轉(zhuǎn)換為十進(jìn)制數(shù)。

十六進(jìn)制轉(zhuǎn)十進(jìn)制

使用 parseInt() 函數(shù),將十六進(jìn)制字符串轉(zhuǎn)換為十進(jìn)制數(shù)。

const hexadecimalString = 'ff';
const decimalNumber = parseInt(hexadecimalString, 16);
console.log(decimalNumber); // 輸出: 255

講解:當(dāng) parseInt() 函數(shù)的第二個(gè)參數(shù)為 16 時(shí),會(huì)將十六進(jìn)制字符串轉(zhuǎn)換為十進(jìn)制數(shù)。

3. 二進(jìn)制、八進(jìn)制、十六進(jìn)制之間的相互轉(zhuǎn)換

可以先將源進(jìn)制轉(zhuǎn)換為十進(jìn)制,再將十進(jìn)制轉(zhuǎn)換為目標(biāo)進(jìn)制。

二進(jìn)制轉(zhuǎn)八進(jìn)制

const binaryString = '1010';
const decimalNumber = parseInt(binaryString, 2);
const octalString = decimalNumber.toString(8);
console.log(octalString); // 輸出: '12'

講解:先使用 parseInt() 函數(shù)將二進(jìn)制字符串轉(zhuǎn)換為十進(jìn)制數(shù),再使用 toString(8) 方法將十進(jìn)制數(shù)轉(zhuǎn)換為八進(jìn)制字符串。

二進(jìn)制轉(zhuǎn)十六進(jìn)制

const binaryString = '11111111';
const decimalNumber = parseInt(binaryString, 2);
const hexadecimalString = decimalNumber.toString(16);
console.log(hexadecimalString); // 輸出: 'ff'

講解:先將二進(jìn)制字符串轉(zhuǎn)換為十進(jìn)制數(shù),再將十進(jìn)制數(shù)轉(zhuǎn)換為十六進(jìn)制字符串。

八進(jìn)制轉(zhuǎn)二進(jìn)制

const octalString = '12';
const decimalNumber = parseInt(octalString, 8);
const binaryString = decimalNumber.toString(2);
console.log(binaryString); // 輸出: '1010'

講解:先將八進(jìn)制字符串轉(zhuǎn)換為十進(jìn)制數(shù),再將十進(jìn)制數(shù)轉(zhuǎn)換為二進(jìn)制字符串。

八進(jìn)制轉(zhuǎn)十六進(jìn)制

const octalString = '377';
const decimalNumber = parseInt(octalString, 8);
const hexadecimalString = decimalNumber.toString(16);
console.log(hexadecimalString); // 輸出: 'ff'

講解:先將八進(jìn)制字符串轉(zhuǎn)換為十進(jìn)制數(shù),再將十進(jìn)制數(shù)轉(zhuǎn)換為十六進(jìn)制字符串。

十六進(jìn)制轉(zhuǎn)二進(jìn)制

const hexadecimalString = 'ff';
const decimalNumber = parseInt(hexadecimalString, 16);
const binaryString = decimalNumber.toString(2);
console.log(binaryString); // 輸出: '11111111'

講解:先將十六進(jìn)制字符串轉(zhuǎn)換為十進(jìn)制數(shù),再將十進(jìn)制數(shù)轉(zhuǎn)換為二進(jìn)制字符串。

十六進(jìn)制轉(zhuǎn)八進(jìn)制

const hexadecimalString = 'ff';
const decimalNumber = parseInt(hexadecimalString, 16);
const octalString = decimalNumber.toString(8);
console.log(octalString); // 輸出: '377'

講解:先將十六進(jìn)制字符串轉(zhuǎn)換為十進(jìn)制數(shù),再將十進(jìn)制數(shù)轉(zhuǎn)換為八進(jìn)制字符串。

綜上所述,通過 toString() 方法和 parseInt() 函數(shù),可以方便地在 JavaScript 中進(jìn)行二進(jìn)制、十進(jìn)制、十六進(jìn)制和八進(jìn)制之間的相互轉(zhuǎn)換。

總結(jié)

到此這篇關(guān)于javascript實(shí)現(xiàn)二進(jìn)制、十進(jìn)制、十六進(jìn)制和八進(jìn)制之間相互轉(zhuǎn)換的文章就介紹到這了,更多相關(guān)js二進(jìn)制、十進(jìn)制、十六進(jìn)制和八進(jìn)制相互轉(zhuǎn)換內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論