Skip to content

Commit 1a3a263

Browse files
Create HexToDecimal.js
Hello. Hope this can be useful to conversions in JavaScript repository :)
1 parent e757a00 commit 1a3a263

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

Conversions/HexToDecimal.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
function hexToInt (hexNum) {
2+
const numArr = hexNum.split('') // converts number to array
3+
numArr.map((item, index) => {
4+
if (!(item > 0)) {
5+
switch (item) {
6+
case 'A': return (numArr[index] = 10)
7+
case 'B': return (numArr[index] = 11)
8+
case 'C': return (numArr[index] = 12)
9+
case 'D': return (numArr[index] = 13)
10+
case 'E': return (numArr[index] = 14)
11+
case 'F': return (numArr[index] = 15)
12+
}
13+
} else numArr[index] = parseInt(item)
14+
})
15+
return numArr // returns an array only with integer numbers
16+
}
17+
18+
function hexToDecimal (hexNum) {
19+
const intItemsArr = hexToInt(hexNum)
20+
return intItemsArr.reduce((accumulator, current, index) => {
21+
return accumulator + (current * Math.pow(16, (intItemsArr.length - (1 + index))))
22+
}, 0)
23+
}
24+
25+
// test cases
26+
console.log(hexToDecimal('5DE9A')) // 384666
27+
console.log(hexToDecimal('3D')) // 61

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy