create function hextoint(@h varchar(8)) returns bigint as 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 endselect dbo.hextoint('B') /* 11 */
这个是 HEX码 我找 BCD码
BCD8421跟16进制的0-9是一样的。
不太清楚转成16进制后高低位的关系,怎么转成10进制帮你改了一下你自己参考一下 if object_id('bcdtoint')>0 drop function bcdtoint go create function bcdtoint (@bcd varchar(8000)) returns varchar(2000) as begin declare @s varchar(4) declare @str varchar(500) set @str='' while len(@bcd)>3 begin set @s=left(@bcd,4) select @str=@str+case when @s='0000' then '0' when @s='0001' then '1' when @s='0010' then '2' when @s='0011' then '3' when @s='0100' then '4' when @s='0101' then '5' when @s='0110' then '6' when @s='0111' then '7' when @s='1000' then '8' when @s='1001' then '9' when @s='1010' then 'A' when @s='1011' then 'B' when @s='1100' then 'C' when @s='1101' then 'D' when @s='1110' then 'E' when @s='1111' then 'F' end set @bcd=right(@bcd,len(@bcd)-4) end declare @r bigint set @r=0 declare @i bigint set @i=1 while @i<=len(@str) begin set @r=@r+ convert(int, ( case when substring(@str,@i,1)<='9' then substring(@str,@i,1) when substring(@str,@i,1)<='A' then '10' when substring(@str,@i,1)<='B' then '11' when substring(@str,@i,1)<='C' then '12' when substring(@str,@i,1)<='D' then '13' when substring(@str,@i,1)<='E' then '14' when substring(@str,@i,1)<='F' then '15' end )) *power(16,len(@str)-@i) set @i=@i+1 endreturn (@r) end goselect dbo.bcdtoint('100110001110') --result /* 2446 */
倒 如果你那个bcd只到9的话那就简单多了 if object_id('bcdtoint')>0 drop function bcdtoint go create function bcdtoint (@bcd varchar(8000)) returns varchar(2000) as begin declare @s varchar(4) declare @str varchar(500) set @str='' while len(@bcd)>3 begin set @s=left(@bcd,4) select @str=@str+case when @s='0000' then '0' when @s='0001' then '1' when @s='0010' then '2' when @s='0011' then '3' when @s='0100' then '4' when @s='0101' then '5' when @s='0110' then '6' when @s='0111' then '7' when @s='1000' then '8' when @s='1001' then '9' end set @bcd=right(@bcd,len(@bcd)-4) endreturn (@str) end goselect dbo.bcdtoint('100110001000')--result /* 988 */
if object_id('bcdtoint')>0 drop function bcdtoint go create function bcdtoint (@bcd varchar(8000)) returns varchar(2000) as begin declare @s varchar(4) declare @str varchar(500) set @str='' while len(@bcd)>3 begin set @s=left(@bcd,4) select @str=@str+case when @s='0000' then '0' when @s='0001' then '1' when @s='0010' then '2' when @s='0011' then '3' when @s='0100' then '4' when @s='0101' then '5' when @s='0110' then '6' when @s='0111' then '7' when @s='1000' then '8' when @s='1001' then '9' end set @bcd=right(@bcd,len(@bcd)-4) endreturn (@str) end goselect dbo.bcdtoint('100110001000')--result /* 988 */
returns bigint
as
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
endselect dbo.hextoint('B')
/*
11
*/
if object_id('bcdtoint')>0
drop function bcdtoint
go
create function bcdtoint (@bcd varchar(8000))
returns varchar(2000)
as
begin
declare @s varchar(4)
declare @str varchar(500)
set @str=''
while len(@bcd)>3
begin
set @s=left(@bcd,4)
select @str=@str+case
when @s='0000' then '0'
when @s='0001' then '1'
when @s='0010' then '2'
when @s='0011' then '3'
when @s='0100' then '4'
when @s='0101' then '5'
when @s='0110' then '6'
when @s='0111' then '7'
when @s='1000' then '8'
when @s='1001' then '9'
when @s='1010' then 'A'
when @s='1011' then 'B'
when @s='1100' then 'C'
when @s='1101' then 'D'
when @s='1110' then 'E'
when @s='1111' then 'F'
end
set @bcd=right(@bcd,len(@bcd)-4)
end
declare @r bigint
set @r=0
declare @i bigint
set @i=1
while @i<=len(@str)
begin
set @r=@r+
convert(int,
(
case
when substring(@str,@i,1)<='9' then substring(@str,@i,1)
when substring(@str,@i,1)<='A' then '10'
when substring(@str,@i,1)<='B' then '11'
when substring(@str,@i,1)<='C' then '12'
when substring(@str,@i,1)<='D' then '13'
when substring(@str,@i,1)<='E' then '14'
when substring(@str,@i,1)<='F' then '15'
end
))
*power(16,len(@str)-@i)
set @i=@i+1
endreturn (@r)
end
goselect dbo.bcdtoint('100110001110')
--result
/*
2446
*/
if object_id('bcdtoint')>0
drop function bcdtoint
go
create function bcdtoint (@bcd varchar(8000))
returns varchar(2000)
as
begin
declare @s varchar(4)
declare @str varchar(500)
set @str=''
while len(@bcd)>3
begin
set @s=left(@bcd,4)
select @str=@str+case
when @s='0000' then '0'
when @s='0001' then '1'
when @s='0010' then '2'
when @s='0011' then '3'
when @s='0100' then '4'
when @s='0101' then '5'
when @s='0110' then '6'
when @s='0111' then '7'
when @s='1000' then '8'
when @s='1001' then '9'
end
set @bcd=right(@bcd,len(@bcd)-4)
endreturn (@str)
end
goselect dbo.bcdtoint('100110001000')--result
/*
988
*/
drop function bcdtoint
go
create function bcdtoint (@bcd varchar(8000))
returns varchar(2000)
as
begin
declare @s varchar(4)
declare @str varchar(500)
set @str=''
while len(@bcd)>3
begin
set @s=left(@bcd,4)
select @str=@str+case
when @s='0000' then '0'
when @s='0001' then '1'
when @s='0010' then '2'
when @s='0011' then '3'
when @s='0100' then '4'
when @s='0101' then '5'
when @s='0110' then '6'
when @s='0111' then '7'
when @s='1000' then '8'
when @s='1001' then '9'
end
set @bcd=right(@bcd,len(@bcd)-4)
endreturn (@str)
end
goselect dbo.bcdtoint('100110001000')--result
/*
988
*/
本来 是用程序来实现 但是NET下性能不好
就想 用SQL 作了