<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Quack Quack</title>
    <description>The latest articles on DEV Community by Quack Quack (@quackquack).</description>
    <link>https://dev.to/quackquack</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1015112%2Fdd685866-369b-4ad9-ad9d-e9873fd924d6.jpeg</url>
      <title>DEV Community: Quack Quack</title>
      <link>https://dev.to/quackquack</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/quackquack"/>
    <language>en</language>
    <item>
      <title>I have created a json inspecting tool. What do you think?</title>
      <dc:creator>Quack Quack</dc:creator>
      <pubDate>Fri, 26 May 2023 16:38:39 +0000</pubDate>
      <link>https://dev.to/quackquack/the-json-inspection-you-dont-know-you-needed-o21</link>
      <guid>https://dev.to/quackquack/the-json-inspection-you-dont-know-you-needed-o21</guid>
      <description>&lt;p&gt;As a back-end developer, when someone ask me why something doesn't work on the UI or something in particular doesn't show incorrect data on the UI. The first thing i do is to open the Developer Console on the browser and capture the response from the API. Then i inspect that JSON and find some particular field doesn't meet the requirements or the data doesn't meet some specific criteria.&lt;/p&gt;

&lt;p&gt;The thing is, inspecting the data using the browser "Ctrl+F" shortcut or copy the whole JSON string then go to VScode or some online json formatter just to find out you have to go through each item in the array of 10 items but has lots of field is very painful. There are other cases where you want to sum a particular field of an array just to get the sum so you could tell if the Front-end guy is calculating the whole thing incorrectly or you are which i find Ctrl+f and json formatter doesn't help at all. So, i created an web app using React js to solve this problem.&lt;/p&gt;

&lt;p&gt;Basically, this website let you input the raw json string and do quick filter, mapping, counting on the provided string in a pipeline fashion. Here's some screenshots&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--SHU32qTl--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fnuhy3saelkq7b6xak0v.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--SHU32qTl--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fnuhy3saelkq7b6xak0v.png" alt="Image description" width="800" height="438"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--IIrYL9oL--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/t49f3yh1r7lnwb7r8xj6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--IIrYL9oL--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/t49f3yh1r7lnwb7r8xj6.png" alt="Image description" width="800" height="438"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--B7g5yeY1--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jhqalwtqheet7cgeh9fk.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--B7g5yeY1--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jhqalwtqheet7cgeh9fk.png" alt="Image description" width="800" height="438"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here's the link to the page: &lt;a href="https://json-pipe.pages.dev/"&gt;https://json-pipe.pages.dev/&lt;/a&gt;&lt;br&gt;
This is just the prototype of the project, i will add more feature to make it able to perform complex operators, please tell me what you think and what i should improve 😉.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>programming</category>
      <category>react</category>
    </item>
    <item>
      <title>Anyone using Remixjs for production? Which UI library are you using?</title>
      <dc:creator>Quack Quack</dc:creator>
      <pubDate>Fri, 14 Apr 2023 13:42:51 +0000</pubDate>
      <link>https://dev.to/quackquack/anyone-using-remixjs-for-production-which-ui-library-are-you-using-2jdb</link>
      <guid>https://dev.to/quackquack/anyone-using-remixjs-for-production-which-ui-library-are-you-using-2jdb</guid>
      <description>&lt;p&gt;First thing first, i'm a .NET developer and have some experience with front end stuff but i could only do the part where i create components add logic to it. If you were tell me to create a react app from scratch or webpack, esbuild, bundling stuff, i have no clues.&lt;/p&gt;

&lt;p&gt;A couple of months ago. I saw their post on some platform and couldn't remember where it was but the post was quite impressive that they claims their way of fetching data was faster. Lately, i've got an idea and want to spin up a website so i gave it a try but been struggling to get the SSR stuff done correctly since React 18 hydration didn't go very well with Antd and Material UI. Moreover, the example on how to mix Remixjs with MaterialUI is outdated. So, i don't have any clues at the moment how could we do that.&lt;/p&gt;

</description>
      <category>javasc</category>
      <category>typescr</category>
      <category>webdev</category>
    </item>
    <item>
      <title>I think we programmer won't lose our job in the next 5 years</title>
      <dc:creator>Quack Quack</dc:creator>
      <pubDate>Thu, 02 Mar 2023 07:40:06 +0000</pubDate>
      <link>https://dev.to/quackquack/i-think-we-programmer-wont-lose-our-job-in-the-next-5-years-2g19</link>
      <guid>https://dev.to/quackquack/i-think-we-programmer-wont-lose-our-job-in-the-next-5-years-2g19</guid>
      <description>&lt;h2&gt;
  
  
  The wild world right now
&lt;/h2&gt;

&lt;p&gt;A wile ago, Microsoft released Github Copilot which is an AI based tool to support people writing code more efficiently using native language.&lt;/p&gt;

&lt;p&gt;Recently, the announcement of ChatGPT marked a new era. An era of AI where we simple just ask without doing much.&lt;/p&gt;

&lt;p&gt;Follow that, Microsoft released Bing which uses newer data than ChatGPT.&lt;/p&gt;

&lt;p&gt;Many creative people tried various things like&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Ask ChatGPT to write unit test for their program, teach them to write code...&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--BniOQBse--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jw57tg3piddj5042fiks.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--BniOQBse--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jw57tg3piddj5042fiks.png" alt="Writing code using ChatGPT" width="640" height="753"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;They even finished an essay within 24 hours with the help of ChatGPT&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now we live in fear of being replace by AI :&amp;lt;&lt;/p&gt;

&lt;h2&gt;
  
  
  What changed?
&lt;/h2&gt;

&lt;p&gt;From my point of view&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;We may switch from using Google to use AI platform to learn stuff quicker. (there is some news that Google is releasing their AI, too).&lt;/li&gt;
&lt;li&gt;We will learn quicker and many people will join the Programming community.&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Speaking of long term, there will be less critical thinking people than ever.&lt;/p&gt;
&lt;h2&gt;
  
  
  What will remain the same?
&lt;/h2&gt;

&lt;p&gt;First, If we bring the value, we get paid.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;From a point of view of "It's just a tool". Remember when people have to write down everything on the paper for accountant then Microsoft Excel came out and people use it as a &lt;strong&gt;Tool&lt;/strong&gt;? Of course, some old people could not catch up and lost their job because the youth are better at learning technology. Nevertheless, they didn't lose their job instantly, they lose their job after a couple of years till all the company shifting from paper to Excel.&lt;br&gt;
People get hired and get paid because they bring the value of accountant done.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;From a point of view of "It's gonna replace us who write code". Yes, If AI does a very good job at coding and could make performance and bug-free app then we should give up coding, why would we reinvent the wheel? We should leverage it's skill to make our products or do whatever brings value instead. We use this to bring the value instead of writing code.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  What should we do?
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Keep learning&lt;/strong&gt; We are still the one who get the job done on a high level. AI at the moment is just a tool which will makes the job done faster but the decision is still up to us. We should use AI to learn even quicker.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Become a high value person&lt;/strong&gt; I don't think AI in the next 5 years could write the whole project from scratch base on some vague requirements. In the meantime, we should try to increase our value so the value we bring up could never be fulfilled by the AI.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Keep learning, keep improving yourself to become a high value person and enjoy your life.&lt;/p&gt;

&lt;p&gt;Tell me your opinion in the comment section!&lt;/p&gt;

</description>
      <category>discuss</category>
      <category>news</category>
      <category>career</category>
      <category>writing</category>
    </item>
    <item>
      <title>Implement base64 encoding using Rust - [Part 2] Handle unicode characters</title>
      <dc:creator>Quack Quack</dc:creator>
      <pubDate>Fri, 17 Feb 2023 02:30:33 +0000</pubDate>
      <link>https://dev.to/quackquack/implement-base64-encoding-using-rust-part-2-handle-unicode-characters-g9o</link>
      <guid>https://dev.to/quackquack/implement-base64-encoding-using-rust-part-2-handle-unicode-characters-g9o</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;In my previous post, i implemented base64 encoding but it only works for non-unicode characters. In this post, we will enhance our &lt;code&gt;base64_encode&lt;/code&gt; function to make it support the whole planet =)).&lt;/p&gt;

&lt;h2&gt;
  
  
  What we did wrong?
&lt;/h2&gt;

&lt;p&gt;Let's have a look at &lt;a href="https://www.rfc-editor.org/rfc/rfc4648" rel="noopener noreferrer"&gt;RFC4648&lt;/a&gt; and find out what we've been missing the whole time.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The encoding process represents &lt;strong&gt;24-bit groups of input bits&lt;/strong&gt; as output&lt;br&gt;
   strings of 4 encoded characters.  Proceeding from left to right, a&lt;br&gt;
   &lt;strong&gt;24-bit input group is formed by concatenating 3 8-bit input groups.&lt;/strong&gt;&lt;br&gt;
   These 24 bits are then treated as 4 concatenated 6-bit groups, each&lt;br&gt;
   of which is translated into a single character in the base 64&lt;br&gt;
   alphabet.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;And a look at our code from previous post&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    for char in input.chars() {
        ...
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;See the bold section from the quote? that's where we went wrong. In other word, we do not guarantee that our character could be represented using 1 byte, only ASCII character could be represented using 1 byte, but unicode is not the case. For example, 'a pile of poo' 💩 is represented using 4 bytes (F0 9F 92 A9).&lt;/p&gt;

&lt;h2&gt;
  
  
  What is the fix?
&lt;/h2&gt;

&lt;p&gt;It's easy to fix, we just need to modify the code to loop over each &lt;code&gt;byte&lt;/code&gt; in the &lt;code&gt;str&lt;/code&gt;. Lucky for us, Rust has a built-in method &lt;code&gt;bytes&lt;/code&gt; which create an &lt;code&gt;Iterator&lt;/code&gt; to loop over each byte in the &lt;code&gt;str&lt;/code&gt;. The good thing about Iterator is that they are lazy and won't do anything until being consumed, we can still keep our memory usage minimal.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    for byte in input.bytes(){

    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Add more testing
&lt;/h2&gt;

&lt;p&gt;A pile of poo 💩 should be encoded correctly to prove the correctness of our program.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;assert_eq!("8J+SqQ==", base64_encode("💩"));
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>performance</category>
      <category>mobile</category>
      <category>native</category>
      <category>productivity</category>
    </item>
    <item>
      <title>Implement base64 encoding using Rust - [Part 1] Base64 for non-unicode characters</title>
      <dc:creator>Quack Quack</dc:creator>
      <pubDate>Wed, 15 Feb 2023 15:04:59 +0000</pubDate>
      <link>https://dev.to/quackquack/implement-base64-encoding-using-rust-part-1-base64-for-non-unicode-characters-4944</link>
      <guid>https://dev.to/quackquack/implement-base64-encoding-using-rust-part-1-base64-for-non-unicode-characters-4944</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;I've been using base64 encoding for a while and haven't got an idea how it works internally so i decided to read some documentation and implement it using Rust.&lt;/p&gt;

&lt;p&gt;What will we learn?&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Some bit wise and bit shift operations&lt;/li&gt;
&lt;li&gt;How base64 works internally?&lt;/li&gt;
&lt;li&gt;Why base64 takes up more space?&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Implementation
&lt;/h2&gt;

&lt;p&gt;First, let's read up the &lt;a href="https://www.rfc-editor.org/rfc/rfc4648" rel="noopener noreferrer"&gt;RFC 4648&lt;/a&gt; to know how things work.&lt;/p&gt;

&lt;h3&gt;
  
  
  How it works
&lt;/h3&gt;

&lt;p&gt;Basically, the base64 encoding takes a chunk of 3 characters and gives back 4 characters by using the following rule. Each character has 8 bits so 3 character has 3*8 = 24 bit, we simple just split that 24 bits into 4 part (6 bits every part) and encode it using the table below to get 4 characters.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdg2k75foa76ho7gbder4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdg2k75foa76ho7gbder4.png" alt="Base64 alphabet"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If we don't have enough character to fill up 3 bytes segment. For example, our input is just an "a" which is 1 byte, we will fill up 2 empty byte 0x0 to get the 3 bytes segment. The first 2 characters should be encoded using "a" first 6 left most bits and 2 right most bits following by 2 padding characters "=".&lt;/p&gt;

&lt;h3&gt;
  
  
  The code
&lt;/h3&gt;

&lt;p&gt;First, we define our alphabet.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;static alphabet: [char; 64] = [
    'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S',
    'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l',
    'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4',
    '5', '6', '7', '8', '9', '+', '/',
];
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And also the padding character&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;static padding_char: char = '=';
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We also need to define the signature of our &lt;code&gt;base64_encode&lt;/code&gt; function. It will take a &lt;code&gt;&amp;amp;str&lt;/code&gt; as input and output a &lt;code&gt;String&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;fn base64_encoding(input:&amp;amp;str) -&amp;gt; String
{
    todo!()
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I initially wanted to loop over the string input and create 3 characters segment first, each group contains a pointer to a portion of the input string. Then i would iterate over each group and process them. But i found that it would consume more memory if the input is a very long string. So, i decided to go with one character at the time.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;for char in input.chars(){
//do stuff here
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Before we can process the list, we need to define some rules to process each character.&lt;/p&gt;

&lt;p&gt;Each character is a byte, which is 0b00000000. (If you don't know about binary numbers, please spend sometimes to get familiar with the concept &lt;a href="https://www.google.com/search?q=binary+numbers+in+computer&amp;amp;client=firefox-b-d&amp;amp;sxsrf=AJOqlzWNgwq7IaH2nDHXP9aR0VgBSi1bMA%3A1676438547804&amp;amp;ei=E2zsY4PQMPTgseMPydGXgA0&amp;amp;ved=0ahUKEwjDpYSh5Jb9AhV0cGwGHcnoBdAQ4dUDCA4&amp;amp;uact=5&amp;amp;oq=binary+numbers+in+computer&amp;amp;gs_lcp=Cgxnd3Mtd2l6LXNlcnAQAzIFCAAQgAQyBQgAEIAEMgUIABCABDIFCAAQgAQyCAgAEBYQHhAKMgsIABAWEB4QDxDxBDIICAAQFhAeEA8yCAgAEBYQHhAPMgkIABAWEB4Q8QQyCQgAEBYQHhDxBDoKCAAQRxDWBBCwAzoHCAAQsAMQQzoECCMQJzoECAAQQzoICAAQsQMQgwE6EQguEIMBEMcBELEDENEDEIAEOggILhCDARCxAzoICAAQgAQQsQM6CwgAEIAEELEDEIMBOg4ILhCABBCxAxDHARDRAzoRCC4QgAQQsQMQgwEQxwEQ0QM6CwguEIAEEMcBEK8BOgsILhCABBCxAxCDAToFCAAQkQI6CggAEIAEEBQQhwI6BggAEBYQHkoECEEYAEoECEYYAFDiDVioLWDZMWgCcAF4AYABkgGIAZcbkgEEMC4yN5gBAKABAcgBCsABAQ&amp;amp;sclient=gws-wiz-serp" rel="noopener noreferrer"&gt;Google&lt;/a&gt;). So, 3 bytes could be represent like this&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;0b00000000 0b00000000 0b00000000.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;If the character is the first one in the group, then we take it's first 6 leftmost bits and encode it as a character.&lt;/p&gt;

&lt;p&gt;0b*&lt;em&gt;000000&lt;/em&gt;*00 0b00000000 0b00000000. (take the bold bits)&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;If the character is the second one in the group, then we take the 2 right most bit of the first character in group and combine it with the 4 left most bits of the second character to get the 2nd encoded character.&lt;/p&gt;

&lt;p&gt;0b000000*&lt;em&gt;00&lt;/em&gt;* 0b*&lt;em&gt;0000&lt;/em&gt;*0000 0b00000000.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;If the character is the third one in the group, then we take the 4 right most bits of the second character and combine it with the 2 left most bits of the third characters to get the 3rd encoded character and take the rest for the 4th encoded character. 3rd encoded character should look like this: 0b00000000 0b0000*&lt;em&gt;0000&lt;/em&gt;* 0b*&lt;em&gt;00&lt;/em&gt;&lt;em&gt;0000 and 4th as follow 0b00000000 0b00000000 0b00&lt;/em&gt;&lt;em&gt;000000&lt;/em&gt;*&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Before we can process inside the loop, we will need to create a couple of variables&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    let mut result = String::new(); //The result to return
    let mut counter = 0; //the current index in the loop, could only be 0 1 2 represent 1st 2nd 3rd byte in the 3 bytes segment
    let mut left_carry: u8 = 0; //bits left from the previous byte

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We will also define the function to handle each character in 3 bytes segment&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//we will return the first encoded char and 
fn handle_first_char(first_char: char) -&amp;gt; (char, u8) {
    let char = alphabet[(first_char as u8 &amp;gt;&amp;gt; 2) as usize]; //take the first 6 bits and encode it as a char;
    let carry = ((first_char as u8) &amp;amp; 0x3) as u8; //last 2 bits;

    (char, carry)
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next, we write 2 separate functions to handle the second and third character&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;fn handle_second_char(second_char: char, carry: u8) -&amp;gt; (char, u8) {
    let char = alphabet[(((second_char as u8) &amp;gt;&amp;gt; 4) | (carry &amp;lt;&amp;lt; 4)) as usize];
    let carry = (second_char as u8) &amp;amp; 0xF;
    (char, carry)
}

fn handle_third_char(third_char: char, carry: u8) -&amp;gt; (char, char) {
    let first_char = alphabet[((carry &amp;lt;&amp;lt; 2) | ((third_char as u8) &amp;gt;&amp;gt; 6)) as usize];
    let second_char = alphabet[((third_char as u8) &amp;amp; 0x3F) as usize];
    (first_char, second_char)
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's go back to our &lt;code&gt;base64_encode&lt;/code&gt; function, we will use these &lt;code&gt;handle_first_character, handle_second_character and handle_third_character&lt;/code&gt; inside this method.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
pub fn base64_encode(input: &amp;amp;str) -&amp;gt; String {
    let mut result = String::new();
    let mut counter = 0;
    let mut left_carry: u8 = 0;

    //return early if the string is empty
    if input.is_empty() {
        return result;
    }

    for char in input.chars() {
        println!("{}", char);
        counter += 1;

        if counter == 1 {
            let (char, carry) = handle_first_char(char);
            result.push(char);
            left_carry = carry;
        }

        if counter == 2 {
            let (char, carry) = handle_second_char(char, left_carry);
            result.push(char);
            left_carry = carry;
        }

        if counter == 3 {
            let (first_char, second_char) = handle_third_char(char, left_carry);

            result.push(first_char);
            result.push(second_char);

            counter = 0; //set counter = 0 to create another 3 byte segment
            left_carry = 0; //reset the carry here
        }
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Add padding characters
&lt;/h4&gt;

&lt;p&gt;The algorithm works now, but it's only correct if our string has enough character to fill up 3 bytes segment without any odd characters. If our string has only one character, it would give us wrong result.&lt;br&gt;
Let's add a bit more code to handle padding.&lt;/p&gt;

&lt;p&gt;We've mentioned above that a segment is made of 3 characters. If we have 1 character left, we should add 2 more characters as padding and add 1 character in case we have 2 characters left.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//from the padding above
fn calculate_padding(counter: u8) -&amp;gt; u8 {
    //the result could only be 1 or 2
    3 - counter
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's add 2 more functions to handle padding for each case (1 or 2 padding char).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;fn handle_padding_first_char(carry: u8) -&amp;gt; char {
    let char = alphabet[(0x00 | (carry &amp;lt;&amp;lt; 4)) as usize];
    char
}

fn handle_padding_second_char(carry: u8) -&amp;gt; char {
    let char = alphabet[((carry &amp;lt;&amp;lt; 2) | (0x00)) as usize];
    char
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And modify our &lt;code&gt;base64_encode&lt;/code&gt; accordingly&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pub fn base64_encode(input: &amp;amp;str) -&amp;gt; String
...
    if counter != 0 {
        let padding = calculate_padding(counter);
        if padding == 2 {
            let char = handle_padding_first_char(left_carry);
            result.push(char);
            result.push(padding_char);
            result.push(padding_char);
        } else {
            let char = handle_padding_second_char(left_carry);
            result.push(char);
            result.push(padding_char);
        }
    }

    println!("{}", result);
    result
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now that's what we call base64 for non-unicode characters.&lt;/p&gt;

&lt;h3&gt;
  
  
  Testing
&lt;/h3&gt;

&lt;p&gt;Let's write some test as well and use an online base64_encode website to verify that we got the correct answer (You can use this &lt;a href="https://www.utilities-online.info/base64" rel="noopener noreferrer"&gt;site&lt;/a&gt;).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn it_works() {
        assert_eq!(&amp;amp;base64_encode("crab"), "Y3JhYg==");
        assert_eq!(
            &amp;amp;base64_encode("the brown fox jump over the lazy dog!"),
            "dGhlIGJyb3duIGZveCBqdW1wIG92ZXIgdGhlIGxhenkgZG9nIQ=="
        );
        assert_eq!(&amp;amp;base64_encode(""), "");
        assert_eq!(&amp;amp;base64_encode("a"), "YQ==");
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;You have just learned:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Bit wise and bit shift operations.&lt;/li&gt;
&lt;li&gt;How base64 works internally.&lt;/li&gt;
&lt;li&gt; The base64 take 3 characters as input then gives back 4 characters as output, that's why it occupies more space than the original string.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;As i mentioned above, our implementation only works for non-unicode characters. In the next part, i will show you guys how to do that.&lt;/p&gt;

&lt;p&gt;The source code is available on Github &lt;a href="https://github.com/oldtune/base64_encode" rel="noopener noreferrer"&gt;here&lt;/a&gt;&lt;/p&gt;

</description>
      <category>rust</category>
      <category>algorithms</category>
      <category>programming</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
