日期:2014-05-18  浏览次数:20454 次

无聊之中用SQL写的进制之间互相转换
SQL code

Create proc HexChange
( @Import int, --输入的进制
  @Export int,--输出的进制
  @Input nvarchar(100),--输入内容
  @Output nvarchar(100) output--输出内容
)
AS
set @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=0
set @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(@import=@Export)
  begin
   print '输入输出的禁止一样,有必要这样吗?'
   set @Output=@Input
  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 @strLength=@strLength-3
            set @j=3
            set @k=0
            while(@j>0)
             begin
              set @k+=CAST(right(@eight,1) as int)*POWER(2,3-@j)
              set @eight=LEFT(@eight,@j-1)
              set @j=@j-1
             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 @i=@i-1
                 set @Input=LEFT(@Input,@i)
                 set @j=@j+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 @strLength=@strLength-1
            set @i=@i+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 @strLength=@strLength-4
            set @j=4
            set @k=0
            while(@j>0)
             begin
              set @k+=CAST(right(@eight,1) as int)*POWER(2,4-@j)
              set @eight=LEFT(@eight,@j-1)
              set @j=@j-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 @Output+=@a
           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 @i=@i-1
                 set @Input=LEFT(@Input,@i)
                 set @j=@j+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'