AGB  ·  Datenschutz  ·  Impressum  







Anmelden
Nützliche Links
Registrieren
Thema durchsuchen
Ansicht
Themen-Optionen

unicodefähiges Base32Decode

Ein Thema von semo · begonnen am 2. Feb 2010 · letzter Beitrag vom 7. Feb 2010
 
Benutzerbild von GPRSNerd
GPRSNerd

Registriert seit: 30. Dez 2004
Ort: Ruhrpott
239 Beiträge
 
Delphi 10.4 Sydney
 
#9

Re: unicodefähiges Base32Decode

  Alt 3. Feb 2010, 13:13
Hier Code von Embarcadero, der einwandfrei auch unter D2010 funktioniert:

Delphi-Quellcode:
const
  ValidChars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ234567';

function FromBase32String(const inString : string) : string;
var
   outString : string;
   aByte : byte;
   bit_buffer,
   inIndex,
   bits_in_buffer,
   outSize,
   pos1, pos2,
   i : integer;
begin
   outSize := (length(inString) * 5 div 8) + 1;
   outString := StringOfChar(' ',outSize);
   pos1 := Pos(inString[1],ValidChars) - 1;
   pos2 := Pos(inString[2],ValidChars) - 1;
   bit_buffer := pos1 or (pos2 shl 5);
   if length(inString) < 3 then begin
      aByte := bit_buffer;
      outString[1] := Chr(aByte);
      result := outString;
      exit;
   end;
   bits_in_buffer := 10;
   inIndex := 3;
   for i := 1 to outSize + 1 do begin
      aByte := bit_buffer;
      outString[i] := Chr(aByte);
      bit_buffer := bit_buffer shr 8;
      bits_in_buffer := bits_in_buffer - 8;
      while (bits_in_buffer < 8) and (inIndex <= length(inString)) do begin
         pos1 := (Pos(inString[inIndex],ValidChars) - 1);
         Inc(inIndex);
         bit_buffer := bit_buffer or (pos1 shl bits_in_buffer);
         bits_in_buffer := bits_in_buffer + 5;
      end;
   end;

   result := outString;
end;

function ToBase32String(const inString : string) : string;
var
   currentChar,
   outString : string;
   inIndex,
   validIndex,
   high : integer;
   aByte,
   currentByte : byte;
begin
   high := 5;
   inIndex := 1;
   while inIndex <= Length(inString) do begin
      currentChar := inString[inIndex];
      currentByte := Ord(currentChar[1]);
      if (high > 8) then begin
         // get the last piece from the current byte, shift it to the right
         // and increment the byte counter
         validIndex := (currentByte shr (high - 5)) mod 256;
         Inc(inIndex);
         currentChar := inString[inIndex];
         currentByte := Ord(currentChar[1]);
         if (inIndex <> Length(inString) + 1) then begin
            // if we are not at the end, get the first piece from
            // the next byte, clear it and shift it to the left
            aByte := (currentByte shl (16 - high)) mod 256;
            validIndex := ((aByte shr 3 ) or validIndex) mod 256;
         end;
         high := high - 3;
      end else if(high = 8) then begin
         Inc(inIndex);
         validIndex := currentByte shr 3;
         high := high - 3;
      end else begin
         // simply get the stuff from the current byte
         aByte := currentByte shl (8 - high) mod 256;
         validIndex := aByte shr 3;
         high := high + 5;
      end;
      currentChar := ValidChars[validIndex + 1];
      outString := outString + currentChar;
   end;
   result := outString;
end;
Ein Vergleich mit den beiden Funktionen von dir lässt auf einen Fehler im Encoder schließen, da der Decoder das gleiche Ergebnis erzeugt.
Stefan
  Mit Zitat antworten Zitat
 


Forumregeln

Es ist dir nicht erlaubt, neue Themen zu verfassen.
Es ist dir nicht erlaubt, auf Beiträge zu antworten.
Es ist dir nicht erlaubt, Anhänge hochzuladen.
Es ist dir nicht erlaubt, deine Beiträge zu bearbeiten.

BB-Code ist an.
Smileys sind an.
[IMG] Code ist an.
HTML-Code ist aus.
Trackbacks are an
Pingbacks are an
Refbacks are aus

Gehe zu:

Impressum · AGB · Datenschutz · Nach oben
Alle Zeitangaben in WEZ +1. Es ist jetzt 23:02 Uhr.
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