Опубликовано: 24 ноя 04
Рейтинг:
Рейтинг:
Автор: Alexey Shirshov (rsdn)
Прислал: Lepsik
create function Hex2Int(@str varchar(10)) returns bigint as begin declare @s char(1) declare @m bigint set @m = 1 declare @sum bigint set @sum = 0 while len(@str) > 0 begin set @s = substring(@str,len(@str),1) declare @j int set @j = case when @s >= '0' and @s <= '9' then cast(@s as int) when @s >= 'A' and @s <= 'F' then ASCII(@s)-ASCII('A') when @s >= 'a' and @s <= 'b' then ASCII(@s)-ASCII('a') else -1 end if @j != -1 begin set @sum = @sum + @j * @m set @m = @m * 10 end set @str = substring(@str,1,len(@str)-1) end return @sum end
Комментарии
Функция перевода из любой системы в любую (от 2 до 36).
create function calcsystem (@source varchar(1000), @fromsystem int, @tosystem int)
returns varchar(1000)
as
begin
declare @dec bigint, @t int, @result varchar(1000), @len int, @c varchar(1)
set @result=''
if (@fromsystem=10)
set @dec=@source
else
begin
set @source=upper(@source)
select @t=1, @dec=0, @len=len(@source)
while (@t<=@len)
begin
set @c=substring(@source,@t,1)
set @dec=@dec*@fromsystem+case when isnumeric(@c)=1 then cast(@c as int) else ascii(@c)-55 end
set @t=@t+1
end
end
if (@tosystem=10)
set @result=@dec
else
begin
while (@dec>0)
begin
set @t=@dec%@tosystem
set @result=case when @t<10 then convert(varchar(1),@t) else char(55+@t) end+@result
set @dec=(@dec-@t)/@tosystem
end
end
return @result
end