Einzelnen Beitrag anzeigen

Kostas

Registriert seit: 14. Mai 2003
Ort: Gerstrhofen
1.060 Beiträge
 
Delphi 10 Seattle Enterprise
 
#1

HexToDec optimieren

  Alt 19. Jun 2014, 19:49
Hallo zusammen,

hier im Forum habe ich die Funktion "HexToDec" gefunden die beliebig lange Hex Zahlen in Dezimal umwandelt.
Als Beispiel eine IMEI:412810079753871 als Hex:01 77 72 D6 29 D6 8F
Ich benötige die gleiche function nur als weitere Variante die als Parameter ein "Array of Byte" bekommt.
Also habe sie umgebaut.
Die function HexToDec addiert einfach nibble für nibble. In meinem Fall "Array of Byte" müsste man Byte für Byte
addieren. Das geht sicherlich einfacher als jedes Byte in das höherwertige nibble bzw. niederwertige nibble zu
zerlegen und zusammen zu addieren.


Delphi-Quellcode:
{--------------------------------------------------------------}
function HexToDec(const s: AnsiString): AnsiString; overload;
//Sehr grosse Hex-Zahlen Decimal umwandeln
var
   total, nibble: TBcd;
   i,n: Integer;
begin
   total := IntegerToBcd(0);
   for i := 1 to Length(s) do
   begin
     n := StrToInt('$' + s[i]);
     nibble := IntegerToBcd(n);
     BcdMultiply(total, 16, total);
     BcdAdd(total, nibble, total);
   end;
   Result := BcdToStr(total);
end;

{--------------------------------------------------------------}
function HexToDec(const s: Array of Byte): AnsiString; overload;
//Sehr grosse Hex-Zahlen Decimal umwandeln
{--------------------------------------------------------------}
var
   total, nibble: TBcd;
   i,n: Integer;
begin
   total := IntegerToBcd(0);
   for i := 0 to Length(s)-1 do
   begin
     n := (s[i] and $F0) shr 4;
     nibble := IntegerToBcd(n);
     BcdMultiply(total, 16, total);
     BcdAdd(total, nibble, total);

     n := (s[i] and $F);
     nibble := IntegerToBcd(n);
     BcdMultiply(total, 16, total);
     BcdAdd(total, nibble, total);

   end;
   Result := BcdToStr(total);
end;
Delphi-Quellcode:
  Label1.caption := HexToDec('017772D629D68F');
  Label1.caption := HexToDec([$1, $77, $72, $D6, $29, $D6, $8F]);
Hat jemand eine Idee?

Gruß Kostas
  Mit Zitat antworten Zitat