# 用sql server进行10进制和16进制的转换 hextoint inttohex

create    function hextoint(@h varchar(8))
returns bigint
begin

declare @r bigint
set @r=0

declare @i bigint
set @i=1

while @i<=len(@h)
begin
set @r=@r+

convert(int,
(
case
when substring(@h,@i,1)<='9' then substring(@h,@i,1)
when substring(@h,@i,1)<='A' then '10'
when substring(@h,@i,1)<='B' then '11'
when substring(@h,@i,1)<='C' then '12'
when substring(@h,@i,1)<='D' then '13'
when substring(@h,@i,1)<='E' then '14'
when substring(@h,@i,1)<='F' then '15'
end
))
*power(16,len(@h)-@i)

set @i=@i+1

end

return @r

end

go

create   function inttohex(@i int)
returns varchar(15)
begin

--declare @i int
--set @i=11259375

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

while @i/16>0
begin

set @r=
(case
when (@i % 16)<=9 then convert(varchar(1),@i % 16)
when (@i % 16)=10 then 'A'
when (@i % 16)=11 then 'B'
when (@i % 16)=12 then 'C'
when (@i % 16)=13 then 'D'
when (@i % 16)=14 then 'E'
when (@i % 16)=15 then 'F'
end)
+@r

--select @r,@i

set @i=@i/16

end

--select @r,@i

if @i>0
set @r=(case
when (@i % 16)<=9 then convert(varchar(1),@i % 16)
when (@i % 16)=10 then 'A'
when (@i % 16)=11 then 'B'
when (@i % 16)=12 then 'C'
when (@i % 16)=13 then 'D'
when (@i % 16)=14 then 'E'
when (@i % 16)=15 then 'F'
end)+@r

-- select @r

return @r

end
go

• 本文已收录于以下专栏：

举报原因： 您举报文章：用sql server进行10进制和16进制的转换 hextoint inttohex 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)