{"id":270,"date":"2025-12-10T10:17:52","date_gmt":"2025-12-10T10:17:52","guid":{"rendered":"https:\/\/cybexpte.com\/?p=270"},"modified":"2025-12-10T10:20:17","modified_gmt":"2025-12-10T10:20:17","slug":"base-encoding-function-creation","status":"publish","type":"post","link":"https:\/\/cybexpte.com\/index.php\/2025\/12\/10\/base-encoding-function-creation\/","title":{"rendered":"Base Encoding Function Creation"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">In most of the machines we use base 64 bytes for encoding or decoding basically anything.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">When coding a function for encoding a base 64, we need to know the basics such as:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The characters in a base 64 which are Numbers <strong>(0-9)<\/strong>, Uppercase Alphabets <strong>(A-Z)<\/strong>, Lowercase Alphabets (a-z) and two Special Characters (+, \/).<\/li>\n\n\n\n<li>Using of variables such as var, let, const.<\/li>\n\n\n\n<li>Operators to use, and etc.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">In this function of basenencode() we will be using function headers as (decoded, baseTo, baseFrom = 10) <em><strong>baseFrom is set to 10 by default parameter as if a specific base will not be given then the default will be used.<\/strong><\/em><br><br><em>e.g:<\/em><br><strong>function basenencode(decoded, baseTo, baseFrom = 10){<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p class=\"wp-block-paragraph\">Then moving on, the Base64String will be defined which will be a static characters of full base 64 characters as shown below:<br><br><strong>const Base64String = &#8216;0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz+\/&#8217;;<\/strong><br><br>This string will be classified as a const variable since it will not change throughout the code at any point.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p class=\"wp-block-paragraph\">After setting a constant string, we will set a variable of encoded to an empty array as it will carry the converted number:<br><br><strong>let encoded = &#8221;;<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p class=\"wp-block-paragraph\">After preparing a output, we will add the main loop which in this case will be a while loop:<br><br><strong>while(decoded > 0){<\/strong><br><br>This loop continues until all digits have been processed.<br>Each loop extracts one digit in the new base.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p class=\"wp-block-paragraph\">Since the loop is added then moving on to the first step will be Finding a Reminder:<br><br><strong>let reminder = decoded % baseTo; <br>console.log(reminder);<\/strong><br><br>The Reminder in the above piece of code which is achieved by a modulo which is a division formula mostly using ( % ) as its operation.<br>So the reminder will be get by decoded modulo (divide by) baseTo.<br>This remainder is the next digit in the new base.<br><em>console.log(reminder);<\/em> is used to preview the output of the variable in console and it is even used for debugging purposes.<br>For example, the decoded value is 45 and the baseTo value is 25 then the reminder that we will get is 20.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p class=\"wp-block-paragraph\">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:<br><br><strong>let c = Base64String[reminder];<br>console.log(c);<\/strong><br><br>We will get the character from Base64String constant by indexing the reminder in a square bracket.<br>For example, since the reminder was 20, it will correspond to the character uppercase alphabet &#8216;<code>K<\/code>&#8216; in the string, so we will be getting the 20th character of the string.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p class=\"wp-block-paragraph\">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:<br><br><strong>encoded = encoded + c;<\/strong><br><br>Where in this we are letting the encoded have a new character which is left before the reminder.<br>This is where we are combining \/ adding the encoded value with the new character.<br><br><strong><em>TO TAKE NOT:<\/em><\/strong><br>For combining the values we have used plus operation (+).<br>In <strong><em>JS<\/em><\/strong> we use plus operation (<strong>+<\/strong>).<br>In <strong><em>PHP<\/em><\/strong> we use dot operation (<strong>.<\/strong>).<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p class=\"wp-block-paragraph\">Once that is done, then we will prepare the next loop which will discard any decimals present such as the code below:<br><br><strong>decoded = Math.floor(decoded \/ baseTo);<\/strong><br><br>This divides the decoded number by the base.<br><em>Math.floor<\/em>  ->   discards any decimals places from the right side of the integer(s) <em>(number)<\/em>.<br><em>Math.ceiling<\/em> ->   discards any decimals places from the left side of the integer(s) <em>(number)<\/em>.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p class=\"wp-block-paragraph\">Lastly, after closing the while loop we are going to return the encoded string:<br><br><strong>console.log(encoded);<br>return encoded;<\/strong><br><br>This is where we return the generated encoded value.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Example Run:<\/strong> <code>basenencode(45, 25)<\/code> &#8212;>  <em>(Values of decoded &#8211; 45 and baseTo &#8211; 25)<\/em><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Step-by-step:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>45 % 25 \u2192 20 \u2192 <code>'K'<\/code><\/li>\n\n\n\n<li>45 \/ 25 \u2192 1<\/li>\n\n\n\n<li>1 % 25 \u2192 1 \u2192 <code>'1'<\/code><\/li>\n\n\n\n<li>1 \/ 25 \u2192 0 \u2192 stop<\/li>\n<\/ul>\n\n\n\n<ol class=\"wp-block-list\"><\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">Output: <code>\"K1\"<\/code><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Summing up, The full Function will Look Like:<\/strong><br><br>function basenencode(decoded, baseTo, baseFrom = 10){<br>   const Base64String = &#8216;0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz+\/&#8217;;<br>   let encoded = &#8221;;<br>   while(decoded > 0){<br>      let reminder = decoded % baseTo;<br>      console.log(reminder);<br>      let c = Base64String[reminder];<br>      console.log(c);<br>      encoded = encoded + c;<br>      decoded = Math.floor(decoded \/ baseTo);<br>   }<br>   console.log(encoded);<br>   return encoded;<br>};<br>basenencode(45, 25);<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>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: In this function of basenencode() we will be using function headers as (decoded, baseTo, baseFrom = 10) baseFrom is set to 10 [&hellip;]<\/p>\n","protected":false},"author":14,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-270","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/cybexpte.com\/index.php\/wp-json\/wp\/v2\/posts\/270","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/cybexpte.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cybexpte.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cybexpte.com\/index.php\/wp-json\/wp\/v2\/users\/14"}],"replies":[{"embeddable":true,"href":"https:\/\/cybexpte.com\/index.php\/wp-json\/wp\/v2\/comments?post=270"}],"version-history":[{"count":4,"href":"https:\/\/cybexpte.com\/index.php\/wp-json\/wp\/v2\/posts\/270\/revisions"}],"predecessor-version":[{"id":294,"href":"https:\/\/cybexpte.com\/index.php\/wp-json\/wp\/v2\/posts\/270\/revisions\/294"}],"wp:attachment":[{"href":"https:\/\/cybexpte.com\/index.php\/wp-json\/wp\/v2\/media?parent=270"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cybexpte.com\/index.php\/wp-json\/wp\/v2\/categories?post=270"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cybexpte.com\/index.php\/wp-json\/wp\/v2\/tags?post=270"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}