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);

    Lesson 1: Encoding

    Introduction

    Their are multiple ways in which you can represent numbers in different systems like binary, hexadecimal, or even Base64? Base conversion is a basic concept in programming, allowing us to represent the same number in multiple ways using different “alphabets” of characters.

    In this post, we’ll explore a small JavaScript function that converts a number from one base to another using a custom set of characters in which it will also help you understand the logic behind base encoding and how you can implement it yourself.


    The Function I Have Studied

    function basenencode(decoded, baseTo, baseForm = 10) {
    const Base64String =’0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/’;
    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);


    What I have Learnt

    1. For finding a reminder we have to use the modulo. Modulo finds the remainder after dividing one integer by another which normally uses %
    For Example:
    X % Y = reminder

    Similar to the below code:

    let reminder = decoded % baseTo;
    console.log (reminder);


    =’0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/’;

    2. The above code is a string which is the “alphabet” that the function uses to represent numbers when converting them to a different base.

    It is arranged in digits + uppercase letters + lowercase letters + +/. It has normally 64 characters


    For Example:
    '0' → 0, '1' → 1, 'A' → 10, 'a' → 36, '+' → 62, '/' → 63

    However, in your string, digits 0–9 are repeated, which is unnecessary and could cause confusion for bases greater than 36.

    For Example:
    If your remainder is 20, the character at index 20 in the string is 'K'. In which that character will represent it’s number 20 in your encoded result.


    3. If you want to Joint a String then just use a + sign. The + sign is used in JS whereas . (dot) is used in PHP for joining String.

    For Example:

    JS – S1 + S2

    PHP – S1 . S2

    Same As Below Function:


    encoded = encoded + c;


    decoded = Math.floor (decoded / baseTo);

    4. The above function updates the number by dividing it by the target base and rounding down. This is part of converting a number to a new base: we take the remainder to get a digit, then reduce the number for the next step. The process repeats until the number becomes zero.


    let declares a variable that can be changed later. Unlike const, you can assign a new value to it.

    String a pattern of characters, like letters, numbers, or symbols, used to represent text.

    Integer is a whole number, a number without any decimal or fractional part.

    Array is a collection of values stored in a single variable. Can store numbers, strings, or even other arrays.


    Summery

    Numbers can be represented in different systems like binary, hexadecimal, or Base64. The basenencode function in JavaScript converts a number to a different base using a custom set of characters. It works by finding remainders with %, mapping them to characters in a string, and reducing the number with Math.floor until it becomes zero. This helps us understand how numbers can be encoded in different bases using strings, integers, arrays, and variables.

    Hello world!

    Welcome to WordPress. This is your first post. Edit or delete it, then start writing!