javascript實(shí)現(xiàn)二進(jìn)制、十進(jìn)制、十六進(jìn)制和八進(jì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)文章希望大家以后多多支持腳本之家!
- JS中字符問題(二進(jìn)制/十進(jìn)制/十六進(jìn)制及ASCII碼之間的轉(zhuǎn)換)
- javascript實(shí)現(xiàn)的字符串與十六進(jìn)制表示字符串相互轉(zhuǎn)換方法
- 使用JavaScript進(jìn)行進(jìn)制轉(zhuǎn)換將字符串轉(zhuǎn)換為十進(jìn)制
- JS中的進(jìn)制轉(zhuǎn)換以及作用
- 用js實(shí)現(xiàn)的十進(jìn)制的顏色值轉(zhuǎn)換成十六進(jìn)制的代碼
- javascript簡(jiǎn)單進(jìn)制轉(zhuǎn)換實(shí)現(xiàn)方法
- JavaScript進(jìn)制轉(zhuǎn)換實(shí)現(xiàn)方法解析
- javascript中簡(jiǎn)單的進(jìn)制轉(zhuǎn)換代碼實(shí)例
相關(guān)文章
js將類數(shù)組對(duì)象轉(zhuǎn)換成數(shù)組對(duì)象
javascript與dom有許多瑕疵,如著名的類數(shù)組對(duì)象Arguments,其他諸如HTMLCollection,NodeList如果它們都是數(shù)組的子類,那多省時(shí)啊。2010-05-05div+css實(shí)現(xiàn)鼠標(biāo)放上去,背景跟圖片都會(huì)變化。
div+css實(shí)現(xiàn)鼠標(biāo)放上去,背景跟圖片都會(huì)變化。...2007-06-062012世界末日倒計(jì)時(shí)代碼 原來沒事虛驚一場(chǎng)
這款時(shí)世界末日倒計(jì)時(shí)源代碼,程序上有JS控制器,是用于網(wǎng)頁中顯示的倒計(jì)時(shí)器,自己可以設(shè)定世界末日的開始時(shí)間,網(wǎng)頁倒計(jì)時(shí)世界末日源代碼直接復(fù)制就可以用2012-12-12