SoFunction
Updated on 2025-03-07

C# URL compression simply implements short URLs

A short URL suddenly appeared, a long URL. After submitting it, there was only a short URL. It seemed quite magical. In fact, it was a very simple thing to analyze it briefly and understand the principle.

There are many types of short URL names called online, including shortening URLs, compressing URLs, etc. To put it bluntly, I will bring you a bag of things to the supermarket to shop. Before entering the supermarket, stuff the things into the supermarket locker, and then get a number plate. If you enter the supermarket, you don’t have to carry a large bag of things. You just need to hold a small number plate, and return the number plate when you come out and take out your backpack. This is the same principle.

Knowing the principle, it is much easier to implement. It is nothing more than receiving a URL and assigning a number. When someone reads this number, we call out the corresponding URL and redirect it, and that's all.
So the table is actually very simple, so simple that it only requires two fields, one self-increment ID and one URL address.

I won't write detailed code here. I believe everyone can do the most basic operations such as simple addition, deletion, modification and search.
Then, we submit a URL and the URL we get is similar: /10086
It seems to be done, but in fact, the self-increment ID we get is a decimal number, and most of the short URLs we see are definitely not all numbers, but letters. After all, if the decimal represents, the data volume will still look a bit long after the amount of data is increased. Then we can use the letters as well. Add numbers in upper and lowercase letters, which is equivalent to 62. Then we also need to implement another method of conversion to compress the ID. Calculation conversion is actually very simple. You can write it out anytime you understand the principle, and you can search it out if you don’t understand it. Here I will list my own implementation. If you have a better implementation, please leave a message to tell me.
Copy the codeThe code is as follows:

static string Number = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
/// <summary>
/// Compress ID ID
/// </summary>
/// <param name="n"></param>
/// <returns></returns>
public string Short(long n) {
string result = ;
int l = ;
while (n / l >= 1) {
result = Number[(int)(n % l)] + result;
n /= l;
}
result = Number[(int)n] + result;
return result;
}
/// <summary>
/// Restore ID ID
/// </summary>
/// <param name="s"></param>
/// <returns></returns>
public long UnShort(string s) {
long result = 0;
if (()) {
s = ();
int l = ;
int m = ;
for (int x = 0; x < l; x++) {
result += (s[l - 1 - x]) * (long)(m, x);
}
}
return result;
}

Then the URL we just now: /10086 becomes: /2CG after compression, with two letters missing. Of course, the larger the number, the more obvious the effect.

OK, after understanding the principle, I believe that it is not difficult for you to build a website with a short URL. The only difficulty is to have a short domain name.
If you think it is helpful to you, please click on the recommendation. If you have any ideas or suggestions, you can leave a message to discuss it together~~~