Number system conversion

Converts numbers between arbitrary number systems (binary, octal, decimal, hexadecimal, etc.).

Diese zwei Funktionen dienen der Umrechnung von Zahlen zwischen verschiedenen Basen. Die all2dez-Funktion konvertiert aus allen Systemen in eine Dezimalzahl und gibt einen Integer-Wert zurück. Die dez2all-Funktion arbeitet in die umgekehrte Richtung und gibt eine Zeichenkette zurück.

Download

ALL2DEZ.INC1.4 KiBQuelltext der all2dez- und dez2all-Funktionen

Contents of the file ALL2DEZ.INC:

{
Konvertiert eine Zahl aus jedem beliebigen Zahlensystem ins Dezimalsystem.
Hex->dezimal hat z.B. einen sys-Wert von 16.

all ist die zu konvertierende Zahl (Ziffern-Werte größer als 9 werden ab "A" abgebildet).
sys ist die Basis der Zahl in 'all'.

Rückgabe ist die Dezimalzahl.
}

function all2dez(all: String; sys: Byte): LongInt;
var
  s    : String;
  ch   : char;
  dez  : LongInt;
  o, x,
  y, z,
  i    : LongInt;

begin
  dez := 0;
  for x := 1 to length(all) do begin
    s := copy(all, x, 1);
    ch := upcase(s[1]);
    o := ord(ch);
    z := 0;
    if (o >= 48) and (o <= 48 + sys) then z := o - 48;
    if sys > 10 then
      if (o >= 65) and (o <= 65 + sys) then
        z := o - 65 + 10;
    i := 1;
    for y := 1 to length(all) - x do begin
      i := i * sys;
    end;
    i := i * z;
    inc(dez, i);
  end;
  all2dez := dez;
end;

{
Konvertiert eine Zahl aus dem Dezimalsystem in jedes beliebige Zahlensystem.
Dezimal->hex hat z.B. einen sys-Wert von 16.

dez ist die zu konvertierende Dezimalzahl.
sys ist die Zielbasis.

Rückgabe ist die Zahldarstellung im Zielsystem.
}

function dez2all(dez: LongInt; sys: Byte): String;
var
  s : String;
  a : String;
  y : Byte;
begin
  y := dez;
  s := '';
  repeat
    y := dez mod sys;
    system.str(y, a);
    if y >= 10 then a := chr(y + 55);
    s := a + s;
    dez := dez div sys;
  until round(dez) / round(sys) = 0;
  dez2all := s;
end;

Licence and terms of use

This software is freely available as source code and compiled version, without restrictions (“public domain”). There is no warranty, not even for merchantability or fitness for a particular purpose. I am not liable for any damage caused through appropriate or inappropriate use.

Statistic data

  • Created on 1996-10-03, updated on 1997-09-23.