how to encode int with base32 in sql server 2008

心不动则不痛 提交于 2019-12-13 17:44:00

问题


i am looking to encode an INT to BASE32 string in SQL Server 2008.

Any suggestions of built-in function or perhaps a custom function?

Thanks


回答1:


Here are a few implementations:

http://dpatrickcaldwell.blogspot.com/2009/05/converting-decimal-to-hexadecimal-with.html

http://snippets.dzone.com/posts/show/707

http://geekswithblogs.net/bbiales/archive/2009/05/04/131732.aspx




回答2:


Pretty sure this will need some debugging but should be close. I translated from a c# function I found that converts base10 to base32.

CREATE FUNCTION dbo.Base10toBase32 (@pInput int)
RETURNS varchar(100)
AS
BEGIN
    Declare @pSet char(32)
    Declare @pRslt varchar(100)
    Declare @pRmdr int
    Declare @pPos int

    SET @pSet = '0123456789ABCDEFGHIJKLMNOPQRSTUV'
    SET @pPos = @pInput

    WHILE @pPos > 0
    BEGIN
        SET @pRmdr = @pPos % 32
        SET @pPos = @pPos / 32
        SET @pRslt = SubString(@pSet,@pRmdr+1,1) + @pRslt
    END

    RETURN @pRslt
END



回答3:


If you're looking for human-readable base32, check out Crockford's: http://www.crockford.com/wrmg/base32.html

Looks like a Dell service tag -- avoids confusion between 1 0 I L etc...



来源:https://stackoverflow.com/questions/5959442/how-to-encode-int-with-base32-in-sql-server-2008

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!