-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnumericalConverter.js
More file actions
138 lines (126 loc) · 5.39 KB
/
numericalConverter.js
File metadata and controls
138 lines (126 loc) · 5.39 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/**
* Number System Converter
*
* This application provides functions to convert numbers between different numerical systems:
* - Decimal (base-10)
* - Binary (base-2)
* - Octal (base-8)
* - Hexadecimal (base-16)
* - This Code is Generated By Gemini AI, user: @develectro
*/
class NumberSystemConverter {
/**
* Converts a decimal number to binary.
* @param {number} decimal - The decimal number to convert.
* @returns {string} The binary representation of the decimal number.
* @throws {Error} If the input is not a non-negative integer.
*/
static decimalToBinary(decimal) {
if (!Number.isInteger(decimal) || decimal < 0) {
throw new Error("Input must be a non-negative integer.");
}
return decimal.toString(2);
}
/**
* Converts a binary number to decimal.
* @param {string} binary - The binary number to convert (string).
* @returns {number} The decimal representation of the binary number.
* @throws {Error} If the input is not a valid binary string.
*/
static binaryToDecimal(binary) {
if (!/^[01]+$/.test(binary)) {
throw new Error("Input must be a valid binary string (containing only 0s and 1s).");
}
return parseInt(binary, 2);
}
/**
* Converts a decimal number to octal.
* @param {number} decimal - The decimal number to convert.
* @returns {string} The octal representation of the decimal number.
* @throws {Error} If the input is not a non-negative integer.
*/
static decimalToOctal(decimal) {
if (!Number.isInteger(decimal) || decimal < 0) {
throw new Error("Input must be a non-negative integer.");
}
return decimal.toString(8);
}
/**
* Converts an octal number to decimal.
* @param {string} octal - The octal number to convert (string).
* @returns {number} The decimal representation of the octal number.
* @throws {Error} If the input is not a valid octal string.
*/
static octalToDecimal(octal) {
if (!/^[0-7]+$/.test(octal)) {
throw new Error("Input must be a valid octal string (containing only digits from 0 to 7).");
}
return parseInt(octal, 8);
}
/**
* Converts a decimal number to hexadecimal.
* @param {number} decimal - The decimal number to convert.
* @returns {string} The hexadecimal representation of the decimal number.
* @throws {Error} If the input is not a non-negative integer.
*/
static decimalToHexadecimal(decimal) {
if (!Number.isInteger(decimal) || decimal < 0) {
throw new Error("Input must be a non-negative integer.");
}
return decimal.toString(16).toUpperCase();
}
/**
* Converts a hexadecimal number to decimal.
* @param {string} hexadecimal - The hexadecimal number to convert (string).
* @returns {number} The decimal representation of the hexadecimal number.
* @throws {Error} If the input is not a valid hexadecimal string.
*/
static hexadecimalToDecimal(hexadecimal) {
if (!/^[0-9A-Fa-f]+$/.test(hexadecimal)) {
throw new Error("Input must be a valid hexadecimal string (containing only digits 0-9 and letters A-F/a-f).");
}
return parseInt(hexadecimal, 16);
}
/**
* Converts binary to hexadecimal
* @param {string} binary - the binary to convert
* @returns {string} - the hexadecimal convert
*/
static binaryToHexadecimal(binary) {
if (!/^[01]+$/.test(binary)) {
throw new Error("Input must be a valid binary string (containing only 0s and 1s).");
}
const decimal = parseInt(binary, 2);
return decimal.toString(16).toUpperCase();
}
/**
* Converts hexadecimal to binary
* @param {string} hexadecimal - the hexadecimal to convert
* @returns {string} - the binary convert
*/
static hexadecimalToBinary(hexadecimal) {
if (!/^[0-9A-Fa-f]+$/.test(hexadecimal)) {
throw new Error("Input must be a valid hexadecimal string (containing only digits 0-9 and letters A-F/a-f).");
}
const decimal = parseInt(hexadecimal, 16);
return decimal.toString(2);
}
}
// Example Usage:
try {
console.log("Decimal to Binary:", NumberSystemConverter.decimalToBinary(10)); // Output: 1010
console.log("Binary to Decimal:", NumberSystemConverter.binaryToDecimal("1010")); // Output: 10
console.log("Decimal to Octal:", NumberSystemConverter.decimalToOctal(25)); // Output: 31
console.log("Octal to Decimal:", NumberSystemConverter.octalToDecimal("31")); // Output: 25
console.log("Decimal to Hexadecimal:", NumberSystemConverter.decimalToHexadecimal(255)); // Output: FF
console.log("Hexadecimal to Decimal:", NumberSystemConverter.hexadecimalToDecimal("FF")); // Output: 255
console.log("Binary to Hexadecimal:", NumberSystemConverter.binaryToHexadecimal("1111"));//output: F
console.log("Hexadecimal to Binary:", NumberSystemConverter.hexadecimalToBinary("F"));//output: 1111
// Example of error handling:
// console.log(NumberSystemConverter.decimalToBinary(-5)); // Throws an error
// console.log(NumberSystemConverter.binaryToDecimal("102")); // Throws an error
// console.log(NumberSystemConverter.octalToDecimal("89")); //throws an error
//console.log(NumberSystemConverter.hexadecimalToDecimal("FG"));//throws an error
} catch (error) {
console.error("Error:", error.message);
}