Base99 converter

A base99 conversion system uses 99 unique characters to represent each digit.

by Abhishek Kumar

HTML

<h1>Base99 Converter</h1>

<textarea class='input-box'></textarea>
<div class='action-bar'>
  <button class='dec_b99-action-btn'>Convert Decimal to Base99</button>
	<button class='b99_dec-action-btn'>Convert Base99 to Decimal</button>
</div>
<textarea class='output-box' disabled></textarea>

<br />
<h3>Define the <i>base99 conversion system.</i></h3>
<p>A <i>base99 conversion system</i> would use 99 distinct symbols to represent numbers. For example, you could use the digits 0-9 and the letters A-Z (lowercase and uppercase) to represent the first 62 symbols. You would then need to choose an additional 37 symbols to represent the remaining values.</p>
<h3>Who created it and why?</h3>
<p><a href="mailto:[email protected]">Abhishek Kumar</a> have created it out of curiosity to convert the date into few characters that could be used as short timestamp.</p>

CSS

h1, h3, p {
	font-family: Garamond, Georgia, 'Times New Roman', serif;
}

.input-box {
	width: 100%;
	display: block;
	height: 25vh;
}

.action-bar {
	display: block;
	margin: 10px 0;
	text-align: center;
}

.action-btn {
		
}

.output-box {
	width: 100%;
	display: block;
	height: 25vh;
}

JavaScript

// base99Characters is a string containing the characters used to represent the digits in the base99 system.
const base99Characters = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz!@#$%^&*()_+-=[]{}|;:,.<>/?`~';

// decimalToBase99 converts a decimal number to its base99 equivalent.
function decimalToBase99(decimalNumber) {
  let result = '';
  while (decimalNumber > 0) {
    let remainder = decimalNumber % 99;
    result = base99Characters[remainder] + result;
    decimalNumber = Math.floor(decimalNumber / 99);
  }
  return result;
}

// base99ToDecimal converts a base99 string to its decimal equivalent.
function base99ToDecimal(base99String) {
  let result = 0;
  for (let i = 0; i < base99String.length; i++) {
    let digitValue = base99Characters.indexOf(base99String[i]);
    result = result * 99 + digitValue;
  }
  return result;
}

function main() {
	let inputBox = document.querySelector('.input-box');
	let outputBox = document.querySelector('.output-box');
	let dec_b99_actionBtn = document.querySelector('.dec_b99-action-btn');
	dec_b99_actionBtn.addEventListener('click', evt => {
	  let iVal = inputBox.value;
		outputBox.value = decimalToBase99(iVal);
	});
	let b99_dec_actionBtn = document.querySelector('.b99_dec-action-btn');
	b99_dec_actionBtn.addEventListener('click', evt => {
	  let iVal = inputBox.value;
		outputBox.value = base99ToDecimal(iVal);
	});
}

main();