C # web site Compression

Source: Internet
Author: User

The short URL suddenly came out. A long URL was submitted, and there was only a short URL. It seemed amazing. In fact, it was a simple analysis, understanding the principles is also very simple.

There are many types of short website names on the Internet, such as the shortening of the website, the compression of the website, and so on. The principle is white, so I brought you a pack of things to the supermarket for shopping, put things into the locker of the supermarket before entering the supermarket, and then get a number card, you do not need to carry a large package of things into the supermarket, just need to hold a small number card, when you come out, I will return the number card and take out your backpack.

Knowing the principle makes it much easier to implement it. It is nothing more than receiving a URL and assigning a number. When someone reads this number, we call the corresponding URL and redirect it, it's done.
Therefore, the table is actually very simple. Simply put, you only need two fields, one auto-incrementing ID and one URL address.

I will not write the detailed code here. The most basic operations such as simple addition, deletion, modification, and query are believed to be common.
Then we commit a URL and get a URL like: http://9520.me/10086
It seems like this is done. In fact, the auto-increment ID we get is a 10-digit number, and the parameters after most of the short URLs we see are definitely not all numbers, but with letters. After all, the 10-digit representation will still look a little long after the data size comes up, so we can also use the letters, uppercase and lowercase letters and numbers, we need to implement another hexadecimal conversion method to compress the ID. The hexadecimal conversion is actually very simple, if you understand the principle, you can easily write it out. If you do not understand it, you can search for it and write it out. Here I will list my own implementations. If you have better implementations, leave a message to tell me.Copy codeThe Code is as follows: static string Number = "0123456789 abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ ";
/// <Summary>
/// Compression ID
/// </Summary>
/// <Param name = "n"> </param>
/// <Returns> </returns>
Public string Short (long n ){
String result = string. Empty;
Int l = Number. Length;
While (n/l> = 1 ){
Result = Number [(int) (n % l)] + result;
N/= l;
}
Result = Number [(int) n] + result;
Return result;
}
/// <Summary>
/// Restore the ID
/// </Summary>
/// <Param name = "s"> </param>
/// <Returns> </returns>
Public long UnShort (string s ){
Long result = 0;
If (s. HasValue ()){
S = s. Trim ();
Int l = s. Length;
Int m = Number. Length;
For (int x = 0; x <l; x ++ ){
Result + = Number. IndexOf (s [l-1-x]) * (long) Math. Pow (m, x );
}
}
Return result;
}

The URL we just mentioned is.

OK. After understanding the principle, I believe that it is not difficult for you to create a website with a short website. What is difficult is simply a short domain name.
If you think it is helpful to you, please click here for recommendations. If you have any ideas or suggestions, please leave a message to discuss them ~~~

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.