DELPHI FAQ: Base64 кодирование


Base64 кодирование
Previous  Home  Next




function
 Decode(const S: AnsiString): AnsiString; 
const
 
  Map: array
[Char] of Byte = (00000000000
    0
0000000000000000000
    0
0000000000062000635253
    54
555657585960610000000012
    3
45678910111213141516171819
    20
21222324250000002627282930
    31
3233343536373839404142434445
    46
4748495051000000000000
    0
0000000000000000000
    0
0000000000000000000
    0
0000000000000000000
    0
0000000000000000000
    0
0000000000000000000
    0
0000000000000000000
    0
); 
var
 
  I: LongInt; 
begin
 
  case
 Length(S) of 
    2
:  
      begin
 
        I := Map[S[1
]] + (Map[S[2]] shl 6); 
        SetLength(Result, 1
); 
        Move(I, Result[1
], Length(Result)) 
      end

    3
:  
      begin
 
        I := Map[S[1
]] + (Map[S[2]] shl 6) + (Map[S[3]] shl 12); 
        SetLength(Result, 2
); 
        Move(I, Result[1
], Length(Result)) 
      end

    4
:  
      begin
 
        I := Map[S[1
]] + (Map[S[2]] shl 6) + (Map[S[3]] shl 12) + 
          (Map[S[4
]] shl 18); 
        SetLength(Result, 3
); 
        Move(I, Result[1
], Length(Result)) 
      end
 
  end
 
end


function
 Encode(const S: AnsiString): AnsiString; 
const
 
  Map: array
[0..63of Char = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' + 
    'abcdefghijklmnopqrstuvwxyz0123456789+/'

var
 
  I: LongInt; 
begin
 
  I := 0

  Move(S[1
], I, Length(S)); 
  case
 Length(S) of 
    1

      Result := Map[I mod
 64] + Map[(I shr 6mod 64]; 
    2

      Result := Map[I mod
 64] + Map[(I shr 6mod 64] + 
        Map[(I shr
 12mod 64]; 
    3

      Result := Map[I mod
 64] + Map[(I shr 6mod 64] + 
        Map[(I shr
 12mod 64] + Map[(I shr 18mod 64
  end
 
end


Взято с сайта http://www.swissdelphicenter.ch/en/tipsindex.php




DELPHI FAQ




EOMY TOP 100      Рейтинг@Mail.ru      Rambler's Top100