Hexabecimal turn to Decimal custom functions
The following is a Hexadecimal turn to Decimal system custom functions.
wrote by Jimmy on Mar 18th 2011
use master
GO
drop function [dbo].[Jimmy_HexadecimalToDecimal]
go
create function [dbo].[Jimmy_HexadecimalToDecimal]
(
@hec varchar(50)
)
returns int
as
begin
declare @dec int,@count int,@bigbit int,@strone int
set @bigbit = 0
set @count = 1
set @dec = 0
--get the number of hexadecimal digits
while @bigbit < len(@hec)
begin
set @strone = ascii(lower(substring(@hec,@count,1)))
if (@strone >= 48 and @strone <= 57)
or
(@strone >= 97 and @strone <= 102)
begin
set @bigbit = @bigbit + 1
set @count = @count + 1
continue
end
else
break
end
set @count = 1
while @bigbit > 0
begin
set @strone =ascii(lower(substring(@hec,@count,1)))
select @dec = @dec +
cast(
case when @strone >=48 and @strone <= 57 then @strone-48
when @strone = 97 then 10
when @strone = 98 then 11
when @strone = 99 then 12
when @strone = 100 then 13
when @strone = 101 then 14
when @strone = 102 then 15
end
as int)*power(16,@bigbit-1)
set @count = @count + 1
set @bigbit = @bigbit - 1
end
return @dec
end
go
----------------------
select [master].[dbo].[Jimmy_HexadecimalToDecimal] ('ab') as '十六进制转十进制' --171
go