Cybex PTE

Loading

All posts by Mishika Chetty

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.