From 4c625f0a6c3068b79131aa0bfc86a024b458a695 Mon Sep 17 00:00:00 2001 From: sarthakgupta1907-cell Date: Mon, 2 Nov 2026 18:35:31 +0530 Subject: [PATCH] feat: add hexadecimal to decimal conversion --- Conversions/HexadecimalToDecimal.js | 29 ++++++++++++ Conversions/test/HexadecimalToDecimal.test.js | 47 +++++++++++++++++++ 2 files changed, 76 insertions(+) create mode 100644 Conversions/HexadecimalToDecimal.js create mode 100644 Conversions/test/HexadecimalToDecimal.test.js diff --git a/Conversions/HexadecimalToDecimal.js b/Conversions/HexadecimalToDecimal.js new file mode 100644 index 0000000000..1680d627c0 --- /dev/null +++ b/Conversions/HexadecimalToDecimal.js @@ -0,0 +1,29 @@ +/** + * @function hexadecimalToDecimal + * @description Converts a hexadecimal number (base-16) string to its decimal + * (base-10) equivalent without using JavaScript's built-in parseInt. + * @param {string} hex - A string representing a hexadecimal number + * @returns {number} - The decimal equivalent of the hexadecimal number + * @see https://en.wikipedia.org/wiki/Hexadecimal + * @example hexadecimalToDecimal("FF") => 255 + * @example hexadecimalToDecimal("10") => 16 + * @example hexadecimalToDecimal("0") => 0 + */ +const hexadecimalToDecimal = (hex) => { + if (typeof hex !== 'string' || !/^[0-9a-fA-F]+$/.test(hex)) { + throw new TypeError( + 'Input must be a valid hexadecimal string containing only 0-9 and A-F' + ) + } + return hex + .toUpperCase() + .split('') + .reverse() + .reduce((acc, char, index) => { + const value = + char >= 'A' ? char.charCodeAt(0) - 55 : char.charCodeAt(0) - 48 + return acc + value * Math.pow(16, index) + }, 0) +} + +export { hexadecimalToDecimal } diff --git a/Conversions/test/HexadecimalToDecimal.test.js b/Conversions/test/HexadecimalToDecimal.test.js new file mode 100644 index 0000000000..c428f376ed --- /dev/null +++ b/Conversions/test/HexadecimalToDecimal.test.js @@ -0,0 +1,47 @@ +import { hexadecimalToDecimal } from '../HexadecimalToDecimal.js' + +describe('hexadecimalToDecimal', () => { + test("hexadecimalToDecimal('0') returns 0", () => { + expect(hexadecimalToDecimal('0')).toBe(0) + }) + + test("hexadecimalToDecimal('A') returns 10", () => { + expect(hexadecimalToDecimal('A')).toBe(10) + }) + + test("hexadecimalToDecimal('F') returns 15", () => { + expect(hexadecimalToDecimal('F')).toBe(15) + }) + + test("hexadecimalToDecimal('10') returns 16", () => { + expect(hexadecimalToDecimal('10')).toBe(16) + }) + + test("hexadecimalToDecimal('FF') returns 255", () => { + expect(hexadecimalToDecimal('FF')).toBe(255) + }) + + test("hexadecimalToDecimal('100') returns 256", () => { + expect(hexadecimalToDecimal('100')).toBe(256) + }) + + test("hexadecimalToDecimal lowercase 'ff' returns 255", () => { + expect(hexadecimalToDecimal('ff')).toBe(255) + }) + + test("hexadecimalToDecimal('1000') returns 4096", () => { + expect(hexadecimalToDecimal('1000')).toBe(4096) + }) + + test('throws TypeError for invalid hex string', () => { + expect(() => hexadecimalToDecimal('GHI')).toThrow(TypeError) + }) + + test('throws TypeError for empty string', () => { + expect(() => hexadecimalToDecimal('')).toThrow(TypeError) + }) + + test('throws TypeError for non-string input', () => { + expect(() => hexadecimalToDecimal(255)).toThrow(TypeError) + }) +})