whats new ¦  programming tips ¦  indy articles ¦  intraweb articles ¦  informations ¦  links ¦  interviews
 misc ¦  tutorials ¦  Add&Win Game

Tips (1541)

Database (90)
Files (137)
Forms (107)
Graphic (114)
IDE (21)
Indy (5)
Internet / LAN (130)
IntraWeb (0)
Math (76)
Misc (126)
Multimedia (45)
Objects/
ActiveX (51)

OpenTools API (3)
Printing (35)
Strings (83)
System (266)
VCL (242)

Top15

Tips sort by
component


Search Tip

Add new Tip

Add&Win Game

Advertising

46 Visitors Online


 
...encode / decode Base64 string?
Autor: Yurii Zhukow
[ Print tip ]  

Tip Rating (183):  
     


const
  
Codes64 = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz+/';

function Encode64(S: string): string;
var
  
i: Integer;
  a: Integer;
  x: Integer;
  b: Integer;
begin
  
Result := '';
  a := 0;
  b := 0;
  for i := 1 to Length(s) do
  begin
    
x := Ord(s[i]);
    b := b * 256 + x;
    a := a + 8;
    while a >= 6 do
    begin
      
a := a - 6;
      x := b div (1 shl a);
      b := b mod (1 shl a);
      Result := Result + Codes64[x + 1];
    end;
  end;
  if a > 0 then
  begin
    
x := b shl (6 - a);
    Result := Result + Codes64[x + 1];
  end;
end;

function Decode64(S: string): string;
var
  
i: Integer;
  a: Integer;
  x: Integer;
  b: Integer;
begin
  
Result := '';
  a := 0;
  b := 0;
  for i := 1 to Length(s) do
  begin
    
x := Pos(s[i], codes64) - 1;
    if x >= 0 then
    begin
      
b := b * 64 + x;
      a := a + 6;
      if a >= 8 then
      begin
        
a := a - 8;
        x := b shr a;
        b := b mod (1 shl a);
        x := x mod 256;
        Result := Result + chr(x);
      end;
    end
    else
      
Exit;
  end;
end;

 

Rate this tip:

poor
very good


Copyright © by SwissDelphiCenter.ch
All trademarks are the sole property of their respective owners