用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
 
select number16,substring(number16,1,1) a,substring(number16,2,1) b, substring(number16,3,1) c, substring(number16,4,1) d, substring(number16,5,1) e,substring(number16,6,1) f, substring(number16,7,1) g, substring(number16,8,1) h, convert(decimal(10,0),isnull(replace(replace(replace(replace(replace(replace(substring(number16,1,1),'A','10'),'B','11'),'C','12'),'D','13'),'E','14'),'F','15'),0)) * power(16,7) + convert(decimal(10,0),isnull(replace(replace(replace(replace(replace(replace(substring(number16,2,1),'A','10'),'B','11'),'C','12'),'D','13'),'E','14'),'F','15'),0)) * power(16,6) + convert(decimal(10,0),isnull(replace(replace(replace(replace(replace(replace(substring(number16,3,1),'A','10'),'B','11'),'C','12'),'D','13'),'E','14'),'F','15'),0)) * power(16,5) + convert(decimal(10,0),isnull(replace(replace(replace(replace(replace(replace(substring(number16,4,1),'A','10'),'B','11'),'C','12'),'D','13'),'E','14'),'F','15'),0)) * power(16,4) + convert(decimal(10,0),isnull(replace(replace(replace(replace(replace(replace(substring(number16,5,1),'A','10'),'B','11'),'C','12'),'D','13'),'E','14'),'F','15'),0)) * power(16,3) + convert(decimal(10,0),isnull(replace(replace(replace(replace(replace(replace(substring(number16,6,1),'A','10'),'B','11'),'C','12'),'D','13'),'E','14'),'F','15'),0)) * power(16,2) + convert(decimal(10,0),isnull(replace(replace(replace(replace(replace(replace(substring(number16,7,1),'A','10'),'B','11'),'C','12'),'D','13'),'E','14'),'F','15'),0)) * power(16,1) + convert(decimal(10,0),isnull(replace(replace(replace(replace(replace(replace(substring(number16,8,1),'A','10'),'B','11'),'C','12'),'D','13'),'E','14'),'F','15'),0)) * power(16,0) from test_16to10 where len(number16)>6 -- create table test_16to10(number16 varchar(10) not null); --ex: number16 value is:'D95B7D3C'
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值