c url compression simple implementation of short url

  • 2020-05-09 19:08:11
  • OfStack

Short url, suddenly 1 suddenly appear out of the thing, long 1 URL, submit in the past, out only a short 1 URL, seems to be quite magical, in fact, simple analysis 1, understand the principle, is also a very simple thing.

Short url name is the name of the Internet has a lot of kinds, url shortening, what url shortener of what, principle to put it bluntly as 1 package to the supermarket shopping, do you have the stuff before into the supermarket to the supermarket store content ark, then get a number, you don't have to back 1 bag into the supermarket, just need to take a small number, out put number and back again, take out your backpack, is 1 point.

Knowing how it works, we can do it a lot easier, just take an URL, assign a number, and when someone reads the number, we'll call out the corresponding URL and redirect it, and we're done.
So the table is really simple, so simple that you only need two fields, one self-incrementing ID, and one URL address.

I'm not going to write the detailed code here, but simply add, delete, change and check the most basic operation I'm sure you can do.
That we submit a URL, the similar: the URL http: / / 9520. me / 10086
Completes the look, actually otherwise, we get the gain ID is a decimal number, and we see most of the short url parameter is not behind the full digital, but took the letter, after all decimal said, after the amount of data to come up will seem a bit long, we can also use the letters, lowercase letters and Numbers, amounts to 62 into the system, then we also need to implement a hexadecimal conversion method to compress ID, hexadecimal conversion is also very simple things, understand the principle of conveniently can be written, Do not understand the random search 1 can also write out, here I put my own implementation list, if you have a better implementation, might as well leave a message to tell me.
 
static string Number = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; 
/// <summary> 
///  The compression ID logo  
/// </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> 
///  reduction ID logo  
/// </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; 
} 

That we have just URL: http: / / 9520. me / 10086, after the compression becomes: http: / / 9520. me / 2 CG, without the two letters, of course, when the greater the number, the more obvious effect.

OK, after understanding the principle, believe that you do a short url of the website is not difficult, it is only difficult to have a short domain name.
If you feel that it is helpful to you, please click 1 to recommend it. If you have any ideas or Suggestions, please leave a message to discuss it

Related articles: