Begin try
Declare @num bigint=1111111110000000111,@rm int,@temp int,@hold int,@a bigint,
@hold1 varchar(50)='',@hold2 varchar(50)=''
, @result bit=0;
set @a=@num
----Check for Negative number----
if(@num<0)
begin
print 'Binary Number Should not be negative form '+cast(@a as varchar)
end
else
--Check for Number is binary form or not------
begin
while(@num!=0)
begin
set @rm=@num%10
set @num=@num/10
if(@rm>1)
begin
set @result=1
set @num=0;
end
end
if(@result=1)
begin
print 'This number is not binary form '+cast(@a as varchar)
end
else
---For Decimal ------------
begin
set @num=@a
set @temp=0
set @hold=0
if(@num=0)
begin
set @num=0
end
while(@num>=1)
begin
set @rm=@num%10
set @num=@num/10
set @hold=@rm*(power(2,@temp))+@hold
set @temp=@temp+1
end
-------- for Octal------------
set @num=@hold
if(@num=0)
begin
set @hold1=@num
end
while(@num>=1)
begin
set @rm=@num%8
set @num=@num/8
set @hold1 =cast(@rm as varchar) +@hold1
set @temp=@temp+1
end
-----For Hexa Decimal----------------
set @num=@hold
if(@num=0)
begin
set @hold2=@num
end
while(@num>=1)
begin
set @rm=@num%16
Declare @a1 varchar(50)
set @a1= (case when cast(@rm as varchar)='10' then 'A'
when cast(@rm as varchar)='11' then 'B'
when cast(@rm as varchar)='12' then 'C'
when cast(@rm as varchar)='13' then 'D'
when cast(@rm as varchar)='14' then 'E'
when cast(@rm as varchar)='15' then 'F'
else cast(@rm as varchar) end)
set @num=@num/16
set @hold2 =cast(@a1 as varchar) +@hold2
set @temp=@temp+1
end
select cast(@a as varchar)'Binary',
cast(@hold as varchar)'Decimal',@hold1'Octal',@hold2'HexaDecimal'
end
end
end try
begin catch
print'Invalid Binary Format'
end catch
Declare @num bigint=1111111110000000111,@rm int,@temp int,@hold int,@a bigint,
@hold1 varchar(50)='',@hold2 varchar(50)=''
, @result bit=0;
set @a=@num
----Check for Negative number----
if(@num<0)
begin
print 'Binary Number Should not be negative form '+cast(@a as varchar)
end
else
--Check for Number is binary form or not------
begin
while(@num!=0)
begin
set @rm=@num%10
set @num=@num/10
if(@rm>1)
begin
set @result=1
set @num=0;
end
end
if(@result=1)
begin
print 'This number is not binary form '+cast(@a as varchar)
end
else
---For Decimal ------------
begin
set @num=@a
set @temp=0
set @hold=0
if(@num=0)
begin
set @num=0
end
while(@num>=1)
begin
set @rm=@num%10
set @num=@num/10
set @hold=@rm*(power(2,@temp))+@hold
set @temp=@temp+1
end
-------- for Octal------------
set @num=@hold
if(@num=0)
begin
set @hold1=@num
end
while(@num>=1)
begin
set @rm=@num%8
set @num=@num/8
set @hold1 =cast(@rm as varchar) +@hold1
set @temp=@temp+1
end
-----For Hexa Decimal----------------
set @num=@hold
if(@num=0)
begin
set @hold2=@num
end
while(@num>=1)
begin
set @rm=@num%16
Declare @a1 varchar(50)
set @a1= (case when cast(@rm as varchar)='10' then 'A'
when cast(@rm as varchar)='11' then 'B'
when cast(@rm as varchar)='12' then 'C'
when cast(@rm as varchar)='13' then 'D'
when cast(@rm as varchar)='14' then 'E'
when cast(@rm as varchar)='15' then 'F'
else cast(@rm as varchar) end)
set @num=@num/16
set @hold2 =cast(@a1 as varchar) +@hold2
set @temp=@temp+1
end
select cast(@a as varchar)'Binary',
cast(@hold as varchar)'Decimal',@hold1'Octal',@hold2'HexaDecimal'
end
end
end try
begin catch
print'Invalid Binary Format'
end catch
No comments :
Post a Comment