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)文章
如何使用pace.js美化你的網(wǎng)站加載進(jìn)度條詳解
Pace.js是一個(gè)非常有意思的js插件,可以自動(dòng)的監(jiān)聽頁面的加載數(shù)據(jù),并且能夠定制加載條,下面這篇文章主要給大家介紹了關(guān)于使用pace.js如何美化你的網(wǎng)站加載進(jìn)度條的相關(guān)資料,需要的朋友可以參考下2022-02-02
js實(shí)現(xiàn)簡(jiǎn)易計(jì)算器小功能
這篇文章主要為大家詳細(xì)介紹了js實(shí)現(xiàn)簡(jiǎn)易計(jì)算器小功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-11-11
js點(diǎn)擊時(shí)關(guān)閉該范圍下拉菜單之外的菜單方法
下面小編就為大家分享一篇js點(diǎn)擊時(shí)關(guān)閉該范圍下拉菜單之外的菜單方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-01-01
JavaScript錯(cuò)誤處理try..catch...finally+涵蓋throw+TypeError+RangeEr
這篇文章主要介紹了JavaScript錯(cuò)誤處理:try..catch...finally+涵蓋throw+TypeError+RangeError,文章內(nèi)容具有一定的參考價(jià)值,需要的小伙伴可以參考一下,希望對(duì)你有所幫助2021-12-12
利用JS實(shí)現(xiàn)數(shù)字增長(zhǎng)
做項(xiàng)目時(shí)候常常遇到,要做一個(gè)數(shù)字滾動(dòng)增加的效果。如何利用JavaScript實(shí)現(xiàn)數(shù)字增長(zhǎng)效果,一起來跟本文學(xué)習(xí)學(xué)習(xí)。2016-07-07

