当前位置: 代码迷 >> Sql Server >> 无聊之中用SQL写的进制之间互相转换,该如何处理
  详细解决方案

无聊之中用SQL写的进制之间互相转换,该如何处理

热度:68   发布时间:2016-04-27 14:48:33.0
无聊之中用SQL写的进制之间互相转换
SQL code
Create proc HexChange( @Import int, --输入的进制  @Export int,--输出的进制  @Input nvarchar(100),--输入内容  @Output nvarchar(100) output--输出内容)ASset @Output=''--定义常规变量declare @strLength int --输入的字符串长度declare @i int --整形变量declare @j int --整形变量declare @k int --整形变量declare @a nvarchar(100)--字符串变量declare @eight nvarchar(100)--截取字符串存储declare @open int --开关1表示输出需要转换0表示不转换set @open=0set @eight=''if(@import not in (2,8,10,16) or @Export not in (2,8,10,16))  begin   print '输入输出的进制有误,请确认!'   set @Output=null  end else if(@[email protected])  begin   print '输入输出的禁止一样,有必要这样吗?'   set @[email protected]  end else if(@Input='0')  begin    set @Output='0'  end else  begin   --输入二进制   if(@import=2)     begin       --输出八进制       if(@Export=8)         begin          set @open=1          set @strLength=LEN(@Input)          set @i=0          while(@strLength>3)           begin            select @eight=right(@Input,3)            set @Input=LEFT(@Input,@strLength-3)            set @[email protected]            set @j=3            set @k=0            while(@j>0)             begin              set @k+=CAST(right(@eight,1) as int)*POWER(2,[email protected])              set @eight=LEFT(@eight,@j-1)              set @[email protected]             end             set @Output+=CAST(@k  as nvarchar(100))           end           set @i=LEN(@Input)           set @j=0           set @k=0           if(@i>0)             begin              while(@i>0)                begin                 set @k+=CAST(right(@Input,1) as int)*POWER(2,@j)                 set @[email protected]                 set @Input=LEFT(@Input,@i)                 set @[email protected]+1                 print @k                end                set @Output+=CAST(@k  as nvarchar(100))                print @Output             end         end        --输出十进制         else if(@Export=10)         begin          set @strLength=LEN(@Input)          set @k=0          set @i=0          while(@strLength>0)           begin            set @k+=CAST(right(@Input,1) as int)*POWER(2,@i)            set @Input=LEFT(@Input,@strLength-1)            set @[email protected]            set @[email protected]+1           end           set @Output=CAST(@k  as nvarchar(100))         end        --输出十六进制        else if(@Export=16)          begin          set @open=1          set @strLength=LEN(@Input)          set @i=0          while(@strLength>4)           begin            select @eight=right(@Input,4)            set @Input=LEFT(@Input,@strLength-4)            set @[email protected]            set @j=4            set @k=0            while(@j>0)             begin              set @k+=CAST(right(@eight,1) as int)*POWER(2,[email protected])              set @eight=LEFT(@eight,@j-1)              set @[email protected]             end             select @a=case @k when 10 then 'A'                               when 11 then 'B'                                when 12 then 'C'                                when 13 then 'D'                                when 14 then 'E'                                when 15 then 'F'                                else CAST(@k as nvarchar(100))end             set @[email protected]           end           set @i=LEN(@Input)           set @j=0           set @k=0           if(@i>0)             begin              while(@i>0)                begin                 set @k+=CAST(right(@Input,1) as int)*POWER(2,@j)                 set @[email protected]                 set @Input=LEFT(@Input,@i)                 set @[email protected]+1                end                select @a=case @k when 10 then 'A'                                  when 11 then 'B'                                   when 12 then 'C'                                   when 13 then 'D'                                   when 14 then 'E'                                   when 15 then 'F'                                   else CAST(@k as nvarchar(100))end                set @[email protected]             end         end              end   --输入八进制   else if(@import=8)     begin     --输出二进制      if(@Export=2)        begin          set @strLength=LEN(@Input)          while(@strLength>0)            begin              set @a=''              select @eight=LEFT(@Input,1)              set @i=CAST(@eight as int)              SET @j=0              while(@j<3)               begin                 set @a+=cast(@i%2 as nvarchar(100))                 set @[email protected]/2                 set @[email protected]+1               end               set @Output+=dbo.reversion(@a)               set @Input=right(@Input,@strLength-1)               set @[email protected]            end        end     --输出十进制      else if(@Export=10)        begin          set @strLength=LEN(@Input)          set @i=1          set @j=0          set @k=0           while(@strLength>0)            begin              select @eight=LEFT(@Input,1)              set @j=CAST(@eight as int)              set @[email protected]*POWER(8,@strLength-1)              set @Input=RIGHT(@Input,@strLength-1)              set @[email protected]            end            set @Output=CAST(@k as nvarchar(100))        end     --输出十六进制      else if(@Export=16)        begin          --转换成二进制          EXEC HexChange 8,2,@Input,@Output output          --由二进制转换成十六进制          EXEC HexChange 2,16,@Output,@Output output        end     end   --输入十进制   else if(@import=10)     begin     --输出二进制       if(@Export=2)         begin           set @open=1           set @i=CAST(@Input as int)           while(@i>0)             begin              set @Output+= CAST(@i%2 as nvarchar(100))              set @[email protected]/2             end         end       --输出八进制       else if(@Export=8)         begin           set @open=1           set @i=CAST(@Input as int)           while(@i>0)             begin              set @Output+= CAST(@i%8 as nvarchar(100))              set @[email protected]/8             end         end       --输出十六进制       else if(@Export=16)         begin           set @open=1           set @i=CAST(@Input as int)           while(@i>0)             begin              set @a=''              set @[email protected]%16              select @a=case @j when 10 then 'A'                                  when 11 then 'B'                                   when 12 then 'C'                                   when 13 then 'D'                                   when 14 then 'E'                                   when 15 then 'F'                                   else CAST(@j as nvarchar(100))end              set @Output+= @a              set @[email protected]/16             end         end     end   --输入十六进制   else if(@import=16)     begin       --输出二进制       if(@Export=2)         begin          set @strLength=LEN(@Input)          while(@strLength>0)            begin              set @a=''              select @eight=LEFT(@Input,1)              select @eight= case @eight                when 'A' then '10'               when 'B' then '11'               when 'C' then '12'               when 'D' then '13'               when 'E' then '14'               when 'F' then '15'               else @eight end              set @i=CAST(@eight as int)              SET @j=0              while(@j<4)               begin                 set @a+=cast(@i%2 as nvarchar(100))                 set @[email protected]/2                 set @[email protected]+1               end               set @Output+=dbo.reversion(@a)               set @Input=right(@Input,@strLength-1)               set @[email protected]            end        end       --输出八进制       else if(@Export=8)         begin           --转换成二进制          EXEC HexChange 16,2,@Input,@Output output          --由二进制转换成八进制          EXEC HexChange 2,8,@Output,@Output output         end       --输出十进制       else if(@Export=10)         begin           --转换成二进制          EXEC HexChange 16,2,@Input,@Output output          --由二进制转换成八进制          EXEC HexChange 2,10,@Output,@Output output         end     end  end  --输出结果  if(@open=1)   begin    select @Output=dbo.reversion(@Output)   end
  相关解决方案