Base Encoding Function Creation
In most of the machines we use base 64 bytes for encoding or decoding basically anything.
When coding a function for encoding a base 64, we need to know the basics such as:
- The characters in a base 64 which are Numbers (0-9), Uppercase Alphabets (A-Z), Lowercase Alphabets (a-z) and two Special Characters (+, /).
- Using of variables such as var, let, const.
- Operators to use, and etc.
In this function of basenencode() we will be using function headers as (decoded, baseTo, baseFrom = 10) baseFrom is set to 10 by default parameter as if a specific base will not be given then the default will be used.
e.g:
function basenencode(decoded, baseTo, baseFrom = 10){
Then moving on, the Base64String will be defined which will be a static characters of full base 64 characters as shown below:
const Base64String = ‘0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz+/’;
This string will be classified as a const variable since it will not change throughout the code at any point.
After setting a constant string, we will set a variable of encoded to an empty array as it will carry the converted number:
let encoded = ”;
After preparing a output, we will add the main loop which in this case will be a while loop:
while(decoded > 0){
This loop continues until all digits have been processed.
Each loop extracts one digit in the new base.
Since the loop is added then moving on to the first step will be Finding a Reminder:
let reminder = decoded % baseTo;
console.log(reminder);
The Reminder in the above piece of code which is achieved by a modulo which is a division formula mostly using ( % ) as its operation.
So the reminder will be get by decoded modulo (divide by) baseTo.
This remainder is the next digit in the new base.
console.log(reminder); is used to preview the output of the variable in console and it is even used for debugging purposes.
For example, the decoded value is 45 and the baseTo value is 25 then the reminder that we will get is 20.
After getting the reminder from running the modulo; the reminder digit that was achieved will be used to pick the character from the string that was put as constant at the beginning of the function:
let c = Base64String[reminder];
console.log(c);
We will get the character from Base64String constant by indexing the reminder in a square bracket.
For example, since the reminder was 20, it will correspond to the character uppercase alphabet ‘K‘ in the string, so we will be getting the 20th character of the string.
When the modulo was applied to get the reminded then we will also get a front number which will be before the reminder left to be operated:
encoded = encoded + c;
Where in this we are letting the encoded have a new character which is left before the reminder.
This is where we are combining / adding the encoded value with the new character.
TO TAKE NOT:
For combining the values we have used plus operation (+).
In JS we use plus operation (+).
In PHP we use dot operation (.).
Once that is done, then we will prepare the next loop which will discard any decimals present such as the code below:
decoded = Math.floor(decoded / baseTo);
This divides the decoded number by the base.
Math.floor -> discards any decimals places from the right side of the integer(s) (number).
Math.ceiling -> discards any decimals places from the left side of the integer(s) (number).
Lastly, after closing the while loop we are going to return the encoded string:
console.log(encoded);
return encoded;
This is where we return the generated encoded value.
Example Run: basenencode(45, 25) —> (Values of decoded – 45 and baseTo – 25)
Step-by-step:
- 45 % 25 → 20 →
'K' - 45 / 25 → 1
- 1 % 25 → 1 →
'1' - 1 / 25 → 0 → stop
Output: "K1"
Summing up, The full Function will Look Like:
function basenencode(decoded, baseTo, baseFrom = 10){
const Base64String = ‘0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz+/’;
let encoded = ”;
while(decoded > 0){
let reminder = decoded % baseTo;
console.log(reminder);
let c = Base64String[reminder];
console.log(c);
encoded = encoded + c;
decoded = Math.floor(decoded / baseTo);
}
console.log(encoded);
return encoded;
};
basenencode(45, 25);



