Delphi-PRAXiS
Seite 2 von 2     12   

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Algorithmen, Datenstrukturen und Klassendesign (https://www.delphipraxis.net/78-algorithmen-datenstrukturen-und-klassendesign/)
-   -   Base32 Implementation (https://www.delphipraxis.net/191675-base32-implementation.html)

hoika 9. Feb 2017 10:48

AW: Base32 Implementation
 
Hallo,
oder es liegt hieran?

https://forums.embarcadero.com/threa...hreadID=109726

hhcm 9. Feb 2017 11:09

AW: Base32 Implementation
 
Zitat:

Zitat von Uwe Raabe (Beitrag 1361156)
Ja, das liegt wohl am Indy-Decoding. So funktioniert es:
Delphi-Quellcode:
uses
  System.SysUtils,
  System.NetEncoding;

const
  base32chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ234567';

function Base32Encode(source: TBytes): string;
var
  i: integer;
  L: Integer;
  nr: int64;
  offset: Integer;
begin
  result := '';
  offset := 0;
  L := length(source);
  while L > 0 do
  begin
    nr := 0;
    for i := 0 to 4 do
    begin
      nr := (nr shl 8);
      if i < L then begin
        nr := nr + source[offset + i];
      end;
    end;
    for i := 7 downto 0 do
      if ((L<2) and (i<6)) or
         ((L<3) and (i<4)) or
         ((L<4) and (i<3)) or
         ((L<5) and (i<1)) then
      result := result + '='
    else
      result := result + base32chars[((nr shr (i*5)) and $1F)+1];
    Inc(Offset, 5);
    Dec(L, Offset);
  end;
end;

procedure Main();
begin
  Assert(Base32Encode(TNetEncoding.Base64.DecodeStringToBytes('DngfhpghKu8='))='BZ4B7BUYEEVO6===');
end;

Da muss man erst mal drauf kommen. Danke.
Bei kurzen Base64 Kodierten Strings funktioniert das hervorragend.
Bei größeren irgendwie nicht mehr.

"Ubz6l2+TFt80EWAf6CTr/Xocpgn9UhOhSvlGMqQsML1LxieEE7bWqB5Y6HAwaC0NSA50swr GHxKs/UOGPS1SJA=="

sollte

"KG6PVF3PSMLN6NARMAP6QJHL7V5BZJQJ7VJBHIKK7FDDFJBMG C6UXRRHQQJ3NVVIDZMOQ4BQNAWQ2SAOOSZQVRQ7CKWP2Q4GHUW VEJA="

werden.
Bricht aber ab. Sollte das nicht wie folgt sein?

Delphi-Quellcode:
Dec(L, offset);
// wird zu
Dec(L, 5);

Uwe Raabe 9. Feb 2017 11:40

AW: Base32 Implementation
 
Zitat:

Zitat von hhcm (Beitrag 1361158)
Sollte das nicht wie folgt sein?

Delphi-Quellcode:
Dec(L, offset);
// wird zu
Dec(L, 5);

Stimmt allerdings. Ungenügende Testabdeckung...

hhcm 9. Feb 2017 11:41

AW: Base32 Implementation
 
Super, Danke !!

p80286 9. Feb 2017 15:38

AW: Base32 Implementation
 
Falls Du noch eine "Second Source" benötigst Bei Google suchenSZCodeBaseX
Allerdings mußt du aufpassen, welche Version angeboten wird, alles unter 1.3.5 ist älter.

Gruß
K-H


Alle Zeitangaben in WEZ +1. Es ist jetzt 05:52 Uhr.
Seite 2 von 2     12   

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024-2025 by Thomas Breitkreuz