Multi-base conversion - using all combinations for URL shortener
Posted
by Guffa
on Stack Overflow
See other posts from Stack Overflow
or by Guffa
Published on 2010-04-29T20:53:45Z
Indexed on
2010/04/29
20:57 UTC
Read the original article
Hit count: 463
I am making an URL shortener, and I am struggling with the optimal way of encoding a number (id) into a character string.
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, ...
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, ...
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.
© Stack Overflow or respective owner