Skip to content

Commit 0f39167

Browse files
committed
Added BinaryToHex Conversion
1 parent 5f601fa commit 0f39167

File tree

4 files changed

+81
-4
lines changed

4 files changed

+81
-4
lines changed

Bit-Manipulation/test/IsPowerOfTwo.test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {IsPowerOfTwo} from '../IsPowerOfTwo'
1+
import { IsPowerOfTwo } from '../IsPowerOfTwo'
22

33
test('Check if 0 is a power of 2 or not:', () => {
44
const res = IsPowerOfTwo(0)

Conversions/BinaryToHex.js

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
const pad = (num, padlen) => {
2+
var pad = new Array(1 + padlen).join(0)
3+
return (pad + num).slice(-pad.length)
4+
}
5+
6+
const hexLookup = (bin) => {
7+
let binary = bin
8+
if (binary.length < 4) {
9+
binary = pad(binary, 4)
10+
}
11+
switch (binary) {
12+
case '0000': return '0'
13+
case '0001': return '1'
14+
case '0010': return '2'
15+
case '0011': return '3'
16+
case '0100': return '4'
17+
case '0101': return '5'
18+
case '0110': return '6'
19+
case '0111': return '7'
20+
case '1000': return '8'
21+
case '1001': return '9'
22+
case '1010': return 'A'
23+
case '1011': return 'B'
24+
case '1100': return 'C'
25+
case '1101': return 'D'
26+
case '1110': return 'E'
27+
case '1111': return 'F'
28+
}
29+
}
30+
const binaryToHex = (binaryString) => {
31+
/*
32+
Function for convertung Binary to Hex
33+
34+
1. The conversion will start from Least Significant Digit (LSB) to the Most Significant Bit (MSB).
35+
2. We divide the bits into sections of 4-bits starting from LSB to MSB.
36+
3. If the MSB get less than 4 bits, then we pad 0s to the front of it.
37+
38+
For Example:
39+
Binary String = '1001101'
40+
41+
1. Divide it to 2 parts => ['100', '1101']
42+
2. Pad 0s the MSB so it'll be => ['0100', '1101']
43+
3. Use the lookup table and merge them, therefore the result is 4D.
44+
45+
*/
46+
47+
let result = ''
48+
binaryString = binaryString.split('')
49+
for (let i = binaryString.length - 1; i >= 0; i = i - 4) {
50+
if (i >= 3) {
51+
result += hexLookup(binaryString.slice(i - 3, i + 1).join(''))
52+
} else {
53+
result += hexLookup(binaryString.slice(0, i + 1).join(''))
54+
}
55+
}
56+
return result.split('').reverse().join('')
57+
}
58+
59+
export default binaryToHex

Conversions/TemperatureConversion.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,7 @@ const fahrenheitToRankine = (fahrenheit) => {
4040
const kelvinToCelsius = (kelvin) => {
4141
// Wikipedia reference: https://en.wikipedia.org/wiki/Kelvin
4242
// Wikipedia reference: https://en.wikipedia.org/wiki/Celsius
43-
return Math.round((kelvin) - 273.15)
44-
43+
return Math.round((kelvin) - 273.15)
4544
}
4645

4746
const kelvinToFahrenheit = (kelvin) => {
@@ -53,7 +52,7 @@ const kelvinToFahrenheit = (kelvin) => {
5352
const kelvinToRankine = (kelvin) => {
5453
// Wikipedia reference: https://en.wikipedia.org/wiki/Kelvin
5554
// Wikipedia reference: https://en.wikipedia.org/wiki/Rankine_scale
56-
return Math.round(( (kelvin) * 9 / 5))
55+
return Math.round(((kelvin) * 9 / 5))
5756
}
5857

5958
const rankineToCelsius = (rankine) => {

Conversions/test/BinaryToHex.test.js

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import binaryToHex from '../BinaryToHex'
2+
3+
describe('BinaryToHex', () => {
4+
it('expects to return correct hexadecimal value', () => {
5+
expect(binaryToHex('1000')).toBe('8')
6+
})
7+
8+
it('expects to return correct hexadecimal value for more than one hex digit', () => {
9+
expect(binaryToHex('11101010')).toBe('EA')
10+
})
11+
12+
it('expects to return correct hexadecimal value for padding-required binary', () => {
13+
expect(binaryToHex('1001101')).toBe('4D')
14+
})
15+
16+
it('expects to return correct hexadecimal value, matching (num).toString(16)', () => {
17+
expect(binaryToHex('1111')).toBe(parseInt('1111', 2).toString(16).toUpperCase())
18+
})
19+
})

0 commit comments

Comments
 (0)