Your question, your audience. Choose who sees your identity—and your question—with question security.
const
Codes64 = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz+/';
....
function EncodeHexToBase64(sInStr: String): String;
const
HEX_BIN_TABLE : array[0..15, 0..3] of Byte =
(
(0,0,0,0), //0
(0,0,0,1), //1
(0,0,1,0), //2
(0,0,1,1), //3
(0,1,0,0), //4
(0,1,0,1), //5
(0,1,1,0), //6
(0,1,1,1), //7
(1,0,0,0), //8
(1,0,0,1), //9
(1,0,1,0), //a
(1,0,1,1), //b
(1,1,0,0), //c
(1,1,0,1), //d
(1,1,1,0), //e
(1,1,1,1) //f
);
var
i, j, b, n: Integer;
arBin: Array of Byte;
begin
Result := '';
SetLength(arBin, Length(sInStr)*4); //allocate space for binary
//make binary array
for i:=1 to Length(sInStr) do
begin
j := 0;
case sInStr[i] of
'A'..'F':
j := (Ord(sInStr[i]) - 55);
'0'..'9':
j := (Ord(sInStr[i]) - 48);
end;
arBin[(i-1)*4+0] := HEX_BIN_TABLE[j, 0];
arBin[(i-1)*4+1] := HEX_BIN_TABLE[j, 1];
arBin[(i-1)*4+2] := HEX_BIN_TABLE[j, 2];
arBin[(i-1)*4+3] := HEX_BIN_TABLE[j, 3];
end;
b := 0;
n := 0;
for i:=Low(arBin) to High(arBin) do
begin
Inc(b);
n := n * 2 + arBin[i];
if b=6 then
begin
Result := Result + Codes64[n+1];
b := 0;
n := 0;
end;
end;
if b>0 then
begin
Result := Result + Codes64[n+1];
end;
end;
function DecodeHexToBase64(sInStr: String): String;
const
HEXSTR : String = '0123456789ABCDEF';
var
i, j, b, n, m: Integer;
arBin: Array of Byte;
begin
Result := '';
SetLength(arBin, (6 * Length(sInStr))); //allocate space
//make binary array
b := 0;
for i:=1 to Length(sInStr) do
begin
n := Pos(sInStr[i], codes64) - 1;
m :=6;
if i=Length(sInStr) then //last char
m := Trunc((Math.Log10(n)/Math.Log10(2)) + 1);
for j:=1 to m do
begin
arBin[b+m-j] := (n mod 2);
n := n div 2;
end;
Inc(b, m);
end;
m := 0;
n := 0;
for i:=Low(arBin) to b-1 do
begin
Inc(m);
n := n * 2 + arBin[i];
if m=4 then
begin
Result := Result + HEXSTR[n+1];
m := 0;
n := 0;
end;
end;
if m>0 then
begin
Result := Result + HEXSTR[n+1];
end;
end;
...
Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.
Have a better answer? Share it in a comment.
What volume of data are you compressing? (how many strings)