Sql Server十六进制转换为十进制

时间:2022-09-17 00:58:19

CREATE  function HexToInt (@hex varchar(100))
returns int
as
begin
    Declare @result int,@iPos int,@iTmp int,@iLoop int,@tmp varchar(16)
    Set @tmp = '0123456789ABCDEF'
    Select @result = 0,@iPos = 0
    While @iPos <   Len(@hex)
    Begin
          Set  @iTmp =
               CharIndex(substring(@hex,Len(@hex) - @iPos,1),@tmp)-1
          Set @iLoop = 1
          While @iLoop <= @iPos and @iTmp > 0
          begin
              Set @iTmp = @iTmp * 16
              Set @iLoop = @iLoop + 1
          end
          Set @result = @result + @iTmp
          Set @iLoop = @iLoop + 1    
          Set @iPos = @iPos + 1
    End

    return @result
End