Top.Mail.Ru
Мастера DELPHI, Delphi programming community Титульная страница Поиск, карта сайта Написать письмо 
| Новости | Поиск | FAQ | Статьи | Книги | Новости VCL |
| Форумы ЧАТ | Орешник | KOL и MCK |
Форумы -» Основная («Начинающим»)/ Базы / WinAPI / Компоненты / Сети / Media / Игры / Corba и COM / KOL / FreePascal / .Net / Прочее / RSDN.ORG

« предыдущая ветвь | форум | следующая ветвь »

Как сконвертить строку в base64? [D6, Win95/98, WinME, NT4, Win2k, WinXP]


serg128 ©   (2006-05-28 11:16:00) 

Как сконвертить строку в base64, но желательно без использования заумных бибилиотек? Приведите код, плиз! :)

Заранее спасибо!


Жуков Олег   (2006-05-28 19:49:00) [1]


> Как сконвертить строку в base64, но желательно без использования
> заумных бибилиотек? Приведите код, плиз! :)

не знаю, есть ли в D6, а в D7 и выше есть EncdDecd.pas
uses EncdDecd;
функции EncodeString, DecodeString, EncodeStream, DecodeStream;


atruhin   (2006-05-28 14:33:00) [2]

Function EncodeBase64( Data: PChar; Len: Integer ): String;
const B64 : array [0..63] of Char = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' +
                                  'abcdefghijklmnopqrstuvwxyz' +
                                  '0123456789+/';
var Ic    : Integer;
  Pi, Po: TPAByte;
  C1    : DWord;
Begin
  if Len > 0 then
  Begin
     SetLength( Result, ( ( Len + 2 ) div 3 ) * 4 );
     Pi := Pointer( Data );
     Po := Pointer( Result );
     for Ic := 1 to Len div 3 do
     Begin
        C1 := Pi^[0] shl 16 + Pi^[1] shl 8 + Pi^[2];
        Po^[0] := Byte( B64[( C1 shr 18 ) and $3f] );
        Po^[1] := Byte( B64[( C1 shr 12 ) and $3f] );
        Po^[2] := Byte( B64[( C1 shr  6 ) and $3f] );
        Po^[3] := Byte( b64[( c1        ) and $3f] );
        Inc( DWord( Po ), 4 );
        Inc( DWord( Pi ), 3 );
     end;
     Case Len mod 3 of
          1: Begin
                C1 := Pi^[0] shl 16;
                Po^[0] := Byte( B64[( C1 shr 18 ) and $3f] );
                Po^[1] := Byte( B64[( C1 shr 12 ) and $3f] );
                Po^[2] := Byte( '=' );
                Po^[3] := Byte('=');
             end;
          2: Begin
                C1 := Pi^[0] shl 16 + Pi^[1] shl 8;
                Po^[0] := Byte( B64[( C1 shr 18 ) and $3f] );
                Po^[1] := Byte( B64[( C1 shr 12 ) and $3f] );
                Po^[2] := Byte( B64[( C1 shr  6 ) and $3f] );
                Po^[3] := Byte('=');
             end;
     end;
  end else Result := '';
end;


atruhin   (2006-05-28 14:33:00) [3]

Function DecodeBase64( Data: PChar; Len: Integer ): String;
var I1, I2: Integer;
  Pi, Po: TPAByte;
  ch1   : Char;
  c1    : DWord;
Begin
  if ( Len > 0 ) and ( Len mod 4 = 0 ) then
  Begin
     Len := Len shr 2;
     SetLength( Result, Len * 3 );
     Pi := Pointer( Data );
     Po := Pointer( Result );
     for I1 := 1 to Len do
     Begin
        C1 := 0;
        I2 := 0;
        While True do
        Begin
           Ch1 := Char( Pi^[I2] );
           Case Ch1 of
                'A'..'Z': C1 := C1 or ( DWord( Ch1 ) - Byte( 'A' )      );
                'a'..'z': C1 := C1 or ( DWord( Ch1 ) - Byte( 'a' ) + 26 );
                '0'..'9': C1 := C1 or ( DWord( Ch1 ) - Byte( '0' ) + 52 );
                '+'     : C1 := C1 or 62;
                '/'     : C1 := C1 or 63;
           else
            Begin
               if I2 = 3 then
               Begin
                  Po^[0] := C1 shr 16;
                  Po^[1] := Byte( C1 shr 8 );
                  SetLength( Result, Length( Result ) - 1 );
               end else
               Begin
                   Po^[0] := C1 shr 10;
                   SetLength( Result, Length( Result ) - 2 );
               end;
               Exit;
            end;
           end;
           if I2 = 3 then break;
           Inc( I2 );
           C1 := C1 shl 6;
        end;
        Po^[0] := C1 shr 16;
        Po^[1] := Byte( C1 shr 8 );
        Po^[2] := Byte( C1 );
        Inc( DWord( Pi ), 4 );
        Inc( DWord( Po ), 3 );
     end;
  end else Result := '';
end;


_n0p   (2006-05-28 13:58:00) [4]

http://www.fourmilab.ch/webtools/base64/rfc1341.html
http://www.fourmilab.ch/webtools/base64/
http://www.google.ru/search?sourceid=navclient-ff&ie=UTF-8&rls=GGGL,GGGL:2005-09,GGGL:ru&q=base64


Наверх

Титульная страница Поиск, карта сайта Написать письмо