"use strict"; module.exports = { slug: "number-base-conversion", title: "Number base conversion", deck_category: "other", description: "Converts a number represented in one base (2 to 36) to its representation in another base.", inputs: [ { name: "value", type: "string", required: true, description: "The number to convert, as digits/letters valid in from_base." }, { name: "from_base", type: "integer", required: true, description: "Base of the input value.", min: 2, max: 36 }, { name: "to_base", type: "integer", required: true, description: "Base to convert the value into.", min: 2, max: 36 } ], outputs: [ { name: "result", description: "value represented in to_base, using digits 0-9 then letters A-Z for digits above 9." } ], steps: [ "Positional notation: a number in base b is a sum of digit_i * b^i, where digits range from 0 to b-1 and digits above 9 are written as letters A (10) through Z (35).", "Parse value as an integer in from_base to get its numeric value.", "Re-express that numeric value in to_base, most significant digit first, using the same digit/letter convention.", "Digits/letters in the output are uppercase." ], constants: [], sources: [ { kind: "edition_page", value: "Knuth, Donald E. The Art of Computer Programming, Volume 2: Seminumerical Algorithms, 3rd ed. Addison-Wesley, 1997, section 4.1 (Positional Number Systems).", title: "The Art of Computer Programming, Vol. 2 (positional number systems)", publisher: "Addison-Wesley", retrieved_utc: "2026-09-30T20:00:00Z", note: "Standard reference for positional notation and base conversion." } ], disclaimer: null, questionText(inputs) { return `What is ${inputs.value} (base ${inputs.from_base}) written in base ${inputs.to_base}?`; }, run(inputs) { const { value, from_base, to_base } = inputs || {}; if (typeof value !== "string" || value.length === 0) throw new Error("value must be a non-empty string"); if (!Number.isInteger(from_base) || from_base < 2 || from_base > 36) throw new Error("from_base must be an integer between 2 and 36"); if (!Number.isInteger(to_base) || to_base < 2 || to_base > 36) throw new Error("to_base must be an integer between 2 and 36"); const n = parseInt(value, from_base); if (Number.isNaN(n)) throw new Error(`value ${value} is not valid in base ${from_base}`); const result = n.toString(to_base).toUpperCase(); return { answer: result, values: { result } }; }, examples: [ { name: "worked example", inputs: { value: "FF", from_base: 16, to_base: 10 }, expected: { answer: "255" }, check: "By hand: hex FF = 15*16 + 15 = 240 + 15 = 255, a well known hexadecimal fact." }, { name: "test vector: decimal to binary", inputs: { value: "255", from_base: 10, to_base: 2 }, expected: { answer: "11111111" }, check: "255 = 2^8 - 1, the well known fact that eight binary 1s equal 255, i.e. 11111111 in binary." } ] };