精华区 [关闭][返回]

当前位置:网易精华区>>讨论区精华>>编程开发>>● Delphi>>其他>>对Base64编码进行解码

主题:对Base64编码进行解码
发信人: delphilxh(先锋)
整理人: soaringbird(2001-07-03 09:40:09), 站内信件
//*************************************************************************
//对Base64编码进行解码
const
  Base64In: TLookup = (
    255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
    255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
    255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
    255, 255, 255, 255,  62, 255, 255, 255,  63,  52,  53,  54,  55,
     56,  57,  58,  59,  60,  61, 255, 255, 255,  64, 255, 255, 255,
      0,   1,   2,   3,   4,   5,   6,   7,   8,   9,  10,  11,  12,
     13,  14,  15,  16,  17,  18,  19,  20,  21,  22,  23,  24,  25,
    255, 255, 255, 255, 255, 255,  26,  27,  28,  29,  30,  31,  32,
     33,  34,  35,  36,  37,  38,  39,  40,  41,  42,  43,  44,  45,
     46,  47,  48,  49,  50,  51, 255, 255, 255, 255, 255
);

function Base64(FCurrentData:string):string;
var
    ByteCount           : Integer;
    SourceIndex         : Integer;
    DataOut,dee             : array[0..2] of Byte;
    DataIn0             : Byte;
    DataIn1             : Byte;
    DataIn2             : Byte;
    DataIn3             : Byte;
    I        : Integer;
    str                 :string;
begin
    Result:='';
    { 跳过空格 }
    SourceIndex := 1;
    while (FCurrentData[SourceIndex] <> #0) and (FCurrentData[SourceIndex] = ' ') do
        Inc(SourceIndex);

    //开始进行解码
    while (FCurrentData[SourceIndex] <> #0) and (FCurrentData[SourceIndex] <> ' ') do
    begin
        DataIn0 := Base64In[Byte(FCurrentData[SourceIndex + 0])];
        DataIn1 := Base64In[Byte(FCurrentData[SourceIndex + 1])];
        DataIn2 := Base64In[Byte(FCurrentData[SourceIndex + 2])];
        DataIn3 := Base64In[Byte(FCurrentData[SourceIndex + 3])];
        DataOut[0] := (DataIn0 and $3F) shl 2 + (DataIn1 and $30) shr 4;
        if DataIn2 <> $40 then
        begin
            DataOut[1] := (DataIn1 and $0F) shl 4 + (DataIn2 and $3C) shr 2;
            if DataIn3 <> $40 then
            begin
                DataOut[2] := (DataIn2 and $03) shl 6 + (DataIn3 and $3F);
                ByteCount := 3;
            end
            else
                ByteCount := 2;
        end
        else
            ByteCount := 1;
        for i:=0 to ByteCount-1 do
            Result:=Result+chr(dataOut[i]);
//        DecodedIndex := DecodedIndex + ByteCount;
        SourceIndex := SourceIndex + 4;
    end;
end;



----
██████ 
█┏━━┓█ Delphi先锋网欢迎您!!!!
█┃之先┃█  北京网易Delphi论坛 
█┃印锋┃█ 
█┗━━┛█ 
██████ 

              

[关闭][返回]