Thema: Hex to Base64

Einzelnen Beitrag anzeigen

gammatester

Registriert seit: 6. Dez 2005
999 Beiträge
 
#4

AW: Hex to Base64

  Alt 7. Mai 2015, 14:51
Wenn Du nicht eine der Base64/Mime-Funktionen zur Verfügung hast, kann man auch mit Bordmitteln arbeiten. Das Program unten liefert die Ausgabe AgGqtgAA
Delphi-Quellcode:
program b64;

{$apptype console}

const
  CT64: array[0..63] of ansichar = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';

{---------------------------------------------------------------------------}
function Base64Str(psrc: pointer; L: integer): ansistring;
  {-Base64 string of memory block of length L pointed by psrc}
var
  q,r: integer;
  b0,b1,b2: byte;
type
  pByte = ^byte;
begin
  result := '';
  if (L>0) and (psrc<>nil) then begin
    q := L div 3;
    r := L mod 3;
    while q>0 do begin
      b0 := pByte(psrc)^; inc(pByte(psrc));
      b1 := pByte(psrc)^; inc(pByte(psrc));
      b2 := pByte(psrc)^; inc(pByte(psrc));
      result := result + CT64[(b0 shr 2) and $3f]
                       + CT64[((b0 shl 4) and $30) or ((b1 shr 4) and $0f)]
                       + CT64[((b1 shl 2) and $3c) or ((b2 shr 6) and $03)]
                       + CT64[b2 and $3f];
      dec(q);
    end;
    if r=2 then begin
      b0 := pByte(psrc)^; inc(pByte(psrc));
      b1 := pByte(psrc)^;
      result := result + CT64[(b0 shr 2) and $3f]
                       + CT64[((b0 shl 4) and $30) or ((b1 shr 4) and $0f)]
                       + CT64[(b1 shl 2) and $3c]
                       + '=';
    end
    else if r=1 then begin
      b0 := pByte(psrc)^;
      result := result + CT64[(b0 shr 2) and $3f]
                       + CT64[(b0 shl 4) and $30]
                       + '==';
    end;
  end;
end;


var
  s: ansistring;
const
  xx: array[0..5] of byte = ($02, $01, $AA, $B6, $00, $00) ;
begin
  s := Base64Str(@xx, sizeof(xx));
  writeln(s);
end.
  Mit Zitat antworten Zitat