From 3c1c057342e3ff715c9543ca6fadc904f915f41c Mon Sep 17 00:00:00 2001 From: Elian Doran Date: Fri, 12 Jul 2024 23:56:39 +0300 Subject: [PATCH] base-converter: Display IEEE with spaces --- src/routes/base-converter/ieee754.svelte | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/routes/base-converter/ieee754.svelte b/src/routes/base-converter/ieee754.svelte index 7564144..08486d7 100644 --- a/src/routes/base-converter/ieee754.svelte +++ b/src/routes/base-converter/ieee754.svelte @@ -6,11 +6,17 @@ let floatValue; let doubleValue; - function formatIeeeResult(buffer) { - return buffer + function formatIeeeResult(buffer, mantissaLength) { + const full = buffer .reduce((str, byte) => str + byte .toString(2) .padStart(8, "0"), ""); + + const signOutput = full.substr(0, 1); + const exponentOutput = full.substr(1, full.length - mantissaLength - 1); + console.log(full.length - mantissaLength - 1); + const mantissaOutput = full.substr(full.length - mantissaLength); + return `${signOutput} ${exponentOutput} ${mantissaOutput}`; } function parseBinaryNotation(input) { @@ -32,10 +38,10 @@ const doubleBuffer = new Uint8Array(8); ieee754.write(floatBuffer, decimalValue, 0, false, 23, 4); - floatValue = formatIeeeResult(floatBuffer); + floatValue = formatIeeeResult(floatBuffer, 23); ieee754.write(doubleBuffer, decimalValue, 0, false, 52, 8); - doubleValue = formatIeeeResult(doubleBuffer); + doubleValue = formatIeeeResult(doubleBuffer, 52); } else { floatValue = 0; }