I found a very creative gadget website. As shown in the cover picture, its function is very simple. It converts a URL address into ooooooooo. You can convert it back to the original address by accessing the converted address. The simple process is shown in the figure below. The conversion logic is a bit like the short link platform, except that this one makes your URL address very long, but it looks like ooooooooo. I was curious about how it was implemented, so I checked the source code. This article interprets its core implementation logic, which is very interesting and clever to implement this function. PrerequisitesBefore we start, let's first learn some knowledge points. Because it involves the conversion between two addresses, which is actually the conversion between strings, some encoding and decoding capabilities will be used. "Convert characters to utf8 array". Each character after conversion has a specific unique value. For example, the utf8 format array after http conversion is [104, 116, 116, 112]. The above is encoding, and the corresponding one below is decoding, "converting utf8 array to string", for example [99, 111, 109] The converted utf8 format array is com. "Represent a Number object as a string in base 4." toString is used more frequently, but there are fewer scenarios where parameters are passed in. The radix parameter is an optional parameter used to specify the base number to be converted, ranging from 2 to 36. If this parameter is not passed in, the decimal base is used by default. "Pad the specified characters on the left side of the string until the string reaches the specified length." The basic syntax is str.padStart(targetLength [, padString]).
URL encoding/decodingNow we will start the URL encoding logic. The core logic is as follows:
There are two key points to explain above. First, what does it mean to map to different forms of o? In fact, the converted o is not one "o", but 4 kinds, but the effect we see with the naked eye is very similar, which can be seen by the characters converted by encodeURI. This actually explains why the above code converts to 4-digit system and pads the left side with 0 to 4 digits. Because the above code defines this.enc as follows, there are only four types of "o" in total, and 4-digit system will only produce 0, 1, 2, and 3, so the converted utf8 characters can be matched one by one to these special "o". The final effect is an example of converting the http character:
The whole conversion process is over. Do you think the design is very good after reading it? After encoding, it is decoding. Decoding is to reverse the above process and restore it to the original URL address. One thing to note here is that parseInt parses 4 characters each time in 4-base mode. at lastThis is the end of sharing the core implementation code. After reading it, you may feel that it is not very complicated. Based on this design, other character effects may be extended. If you are interested, you can try it. Sharing the transcoded address with your friends will definitely bring different surprises. The following is an AI gadget address I converted. Click to see the effect~ Official website address: "ooooooooooooooooooooooooo.ooo" ooooooooooooooooooooo.ooo/ooooоооoоᴏοoοᴏοoοᴏooοᴏoᴏoᴏооoоᴏᴏoо ᴏᴏоооᴏооoᴏoоᴏоооᴏоооoоооᴏоооoᴏоооoоооοооᴏооооᴏoоᴏооооооᴏооооᴏо |
<<: Six advantages of single-pair Ethernet technology
>>: I am confused. If I want to store IP addresses, what data type is better?
UFOVPS's latest promotion will only last for ...
New network deployments and enterprise momentum a...
Looking back at the year 2020, there are many eve...
The 16th Middle School of Yueyang City, Hunan Pro...
On October 15, the "2021 Financial Cybersecu...
Characteristics and application environment of wi...
In the past year since 5G was put into commercial...
Expanding the presence of fiber optics has become...
With the development of 5G networks, everyone has...
Introduction RabbitMQ is an open source message b...
"5G brings a lot of data, but the question i...
What comes to mind when you hear the word “automa...
Wireless charging is very popular recently, and m...
This month, we have shared RAKsmart's New Yea...
It is predicted that by 2025, there will be 1.2 b...