多基准转换-使用所有组合的URL缩短器 [英] Multi-base conversion - using all combinations for URL shortener

查看:113
本文介绍了多基准转换-使用所有组合的URL缩短器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做一个URL缩短器,并且正在努力将数字(id)编码为字符串的最佳方法.

I am making an URL shortener, and I am struggling with the optimal way of encoding a number (id) into a character string.

我使用的字符是0-9,A-Z,a-z,因此基本上是base-62编码.那是很基本的,但是并没有利用所有可能的代码.它将产生的代码为:

I am using the characters 0-9,A-Z,a-z so it will basically be a base-62 encoding. That is pretty basic, but it doesn't make use of all possible codes. The codes that it would produce would be:

0, 1, ... y, z, 10, 11, ... zy, zz, 100, 101, ...

请注意,未使用代码00到0z,对于000到0zz相同,依此类推.我想使用所有代码,例如:

Notice that the codes 00 to 0z is not used, the same for 000 to 0zz, and so on. I would like to use all the codes, like this:

0, 1, ... y, z, 00, 01, ... zy, zz, 000, 001, ...

这将是base-62和base-63的某种组合,根据位置的不同而有所不同...使用base-62很容易,例如:

It would be some combination of base-62 and base-63, with different bases depending on the position... Using base-62 is easy, for example:

create procedure tiny_GetCode
    @UrlId int
as
set nocount on

declare @Code varchar(10)
set @Code = ''

while (@UrlId > 0 or len(@Code) = 0) begin
    set @Code = substring('0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz', @UrlId % 62 + 1, 1) + @Code
    set @UrlId = @UrlId / 62
end

select @Code

但是我还没有设法利用它来进行多基转换,以利用所有代码.

But I haven't yet managed to make a multi-base conversion out of it, to make use of all the codes.

推荐答案

我设法进行了转换.棘手的是,它不仅是混合的基数转换,而且第一个字符的基数较高还会影响较长代码的值.

I managed to make the conversion. The tricky thing is that it's not just a mixed base conversion, the higher base of the first character also affects the values of longer codes.

我从一个简单的案例开始;以10为基的代码.我看到两位数范围有10个额外的代码,三位数范围有100个额外的代码,依此类推:

I started with an easier case; base-10 codes. I saw that the two digit range has 10 extra codes, the three digit range has 100 extra codes, and so on:

0 - 9        : '0' - '9'
10 - 109     : '00' - '99'
110 - 1109   : '000' - '999'
1110 - 11109 : '0000' - '9999'

因此,代码中第一个字符的值不仅是升至该位置的基数,而且还具有偏移量.

So, the value of the first character in the code is not just the base raised to the position, but it also has an offset.

将其应用于base-62编码后,这就是我最终得到的结果:

After applying this to the base-62 encoding, this is what I ended up with:

create function tiny_Encode(@UrlId int) returns varchar(10)
as
begin

  declare
    @Chars varchar(62),
    @Code varchar(10),
    @Value int,
    @Adder int

  set @Chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'
  if (@UrlId < 63) begin
    set @Code = substring(@Chars, @UrlId, 1)
  end else begin
    set @UrlId = @UrlId - 1
    set @Value = 62
    set @Adder = 0
    while (@UrlId >= @Value * 63 + @Adder) begin
      set @Adder = @Adder + @Value
      set @Value = @Value * 62
    end
    set @Code = substring(@Chars, (@UrlId - @Adder) / @Value, 1)
    set @UrlId = ((@UrlId - @Adder) % @Value)
    while (@Value > 1) begin
      set @Value = @Value / 62
      set @Code = @Code + substring(@Chars, @UrlId / @Value + 1, 1)
      set @UrlId = @UrlId % @Value
    end
  end
  return @Code

end

这篇关于多基准转换-使用所有组合的URL缩短器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆