标题:转摘 SQL2000 Base64编码解码方法
只看楼主
mywisdom88
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:190
帖 子:3125
专家分:8340
注 册:2015-3-25
结帖率:98.96%
 问题点数:0 回复次数:0 
转摘 SQL2000 Base64编码解码方法
-- SQL2000 Base64编码解码
create function dbo.base64_decode(@encoded_text varchar(8000))
  returns varchar(6000)
  as
  begin
    declare
      @output varchar(8000),  
      @block_start int,  
      @encoded_length int,  
      @decoded_length int,  
      @mapr binary(122)
    set @output=''
    set @mapr = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF   --  1-33
              + 0xFFFFFFFFFFFFFFFFFFFF3EFFFFFF3F3435363738393A3B3C3DFFFFFF00FFFFFF   --  33-64
              + 0x000102030405060708090A0B0C0D0E0F10111213141516171819FFFFFFFFFFFF   --  65-96
              + 0x1A1B1C1D1E1F202122232425262728292A2B2C2D2E2F30313233               --  97-122

    set @encoded_length = len(@encoded_text)
    set @decoded_length = @encoded_length /4 *3

    set @block_start = 1
    while @block_start < @encoded_length
    begin
      -- decode the block and add to output  
      -- binary values between 1 and 4 bytes can be implicitly cast to INT  
      set @output = @output + CAST(CAST(CAST(
          substring(@mapr,ascii(substring(@encoded_text,@block_start,1)),1) * 262144
        + substring(@mapr,ascii(substring(@encoded_text,@block_start + 1,1)),1) * 4096
        + substring(@mapr,ascii(substring(@encoded_text,@block_start + 2,1)),1) * 64
        + substring(@mapr,ascii(substring(@encoded_text,@block_start + 3,1)),1)
        as integer) as binary(3)) as varchar(3))
      set @block_start = @block_start + 4  
    end
    if right(@encoded_text,2) = '=='
       set @decoded_length = @decoded_length - 2
    else if right(@encoded_text,1) = '='
       set @decoded_length = @decoded_length - 1
    return left(@output,@decoded_length)
  end
搜索更多相关主题的帖子: -- varchar output int set 
2018-03-28 10:33



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-485616-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.014948 second(s), 7 queries.
Copyright©2004-2025, BCCN.NET, All Rights Reserved