function BytesToText64(PC
Char;Len:Integer):String;
const
MapArray:array[0..63] of char=('0','1','2','3','4','5','6','7','8','9',
'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s',
't','u','v','w','x','y','z',
'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S',
'T','U','V','W','X','Y','Z','+','-'); //增加 '+','-'
var
i,n:Integer;
b,b1:Byte;
p,pDest
Char;
begin
n:=Len*4 div 3;
if Len mod 3>0 then
Inc
;
SetLength(Result,n);
p:=PC;
pDest:=@Result[1];
for i:=1 to Len div 3 do
begin
b:=Byte(p^);
pDest^:=MapArray[b shr 2];
Inc(pDest);
Inc(p);
b1:=Byte(p^);
pDest^:=MapArray[((b and $03) shl 4) or (b1 shr 4)];
Inc(pDest);
Inc(p);
b:=Byte(p^);
pDest^:=MapArray[((b1 and $0F) shl 2) or (b shr 6)];
Inc(pDest);
pDest^:=MapArray[b and $3F];
Inc(pDest);
Inc(p);
end;
n:=Len mod 3;
if n=0 then
exit;
b:=Byte(p^);
pDest^:=MapArray[b shr 2];
Inc(pDest);
if n=1 then
begin
pDest^:=MapArray[(b and $03) shl 4];
exit;
end;
Inc(p);
b1:=Byte(p^);
pDest^:=MapArray[((b and $03) shl 4) or (b1 shr 4)];
Inc(pDest);
pDest^:=MapArray[(b1 and $0F) shl 2];
end;
function Text64ToBytes(PC
Char;Len:Integer):String;
function Char64ToByte(Ch:Char):Byte;
begin
case Ch of
'0'..'9':
Result:=Byte(Ch)-Byte('0');
'a'..'z':
Result:=Byte(Ch)-Byte('a')+10;
'A'..'Z':
Result:=Byte(Ch)-Byte('A')+36;
'+':
Result:=62;
else
Result:=63;
end;
end;
var
i,n:Integer;
b,b1:Byte;
p,pDest
Char;
begin
n:=((Len-1) div 4)*3+(Len-1) mod 4;
SetLength(Result,n);
p:=PC;
pDest:=@Result[1];
for i:=1 to Len div 4 do
begin
b:=Char64ToByte(p^);
Inc(p);
b1:=Char64ToByte(p^);
pDest^:=Char((b shl 2) or (b1 shr 4));
Inc(p);
b:=Char64ToByte(p^);
Inc(pDest);
pDest^:=Char((b1 shl 4) or (b shr 2));
Inc(p);
b1:=Char64ToByte(p^);
Inc(pDest);
pDest^:=Char((b shl 6) or b1);
Inc(p);
Inc(pDest);
end;
n:=Len mod 4;
if n=0 then
exit;
b:=Char64ToByte(p^);
Inc(p);
b1:=Char64ToByte(p^);
pDest^:=Char((b shl 2) or (b1 shr 4));
if n=3 then
begin
Inc(p);
b:=Char64ToByte(p^);
Inc(pDest);
pDest^:=Char((b1 shl 4) or (b shr 2));
end;
end;
//eg:
procedure TForm1.Button1Click(Sender: TObject);
begin
Edit2.Text:=BytesToText64(@Edit1.Text[1],Length(Edit1.Text));
end;
procedure TForm1.Edit2Change(Sender: TObject);
begin
Edit3.Text:=Text64ToBytes(@Edit2.Text[1],Length(Edit2.Text));
end;
正在处理序列号...