-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharithmetic.js
More file actions
58 lines (37 loc) · 1.54 KB
/
arithmetic.js
File metadata and controls
58 lines (37 loc) · 1.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
const octalToBinaryObj = {'0':'000', '1':'001','2':'010','3':'011','4':'100','5':'101', '6':'110','7':'111'};
function octalToBinary(octalStr){
const binArr = octalStr.split('').map( octal => octalToBinaryObj[octal]);
const binStr = binArr.join('');
return binStr;
}
const binaryToHexObj = {'0000':'0', '0001':'1', '0010':'2', '0011':'3', '0100':'4', '0101':'5', '0110':'6', '0111':'7', '1000':'8', '1001':'9', '1010':'A', '1011':'B', '1100':'C', '1101':'D', '1110':'E', '1111':'F'}
function binaryToHex(binStr){
const four = 4;
const zero = '0';
// binStrCopy is binStr paded with '0' if needed.
// Paddig binStrCopy start with '0' ensures binStr.length is evenly divisible by 4
const binStrCopy = ''.padStart( four - ( binStr.length % four ) , zero ) + binStr;
// since binStrCopy.length is evenly divisible by 4,
// hexArr will contain string of size 4 at each index
const hexArr = binStrCopy.match(/.{1,4}/g).map( bin => binaryToHexObj[ bin ] );
// remove the leading zero, unless '0' is the only element
if(hexArr[0] === '0' && hexArr.length > 1){
hexArr.shift();
}
const hexStr = hexArr.join('');
return hexStr;
}
function octalToHex(octalStr){
const binaryStr = octalToBinary(octalStr);
const hexStr = binaryToHex(binaryStr);
return hexStr;
}
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.on('line', (line) => {
const res = octalToHex(line);
console.log(res);
});