Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Object-Pascal / Delphi-Language (https://www.delphipraxis.net/32-object-pascal-delphi-language/)
-   -   Delphi Bin in Dec umwandeln (https://www.delphipraxis.net/3875-bin-dec-umwandeln.html)

Quick_silver 3. Apr 2003 18:04


Bin in Dec umwandeln
 
also ich will eine Binäre Zahl (Z.b. aus einer schnittstelle) in eine Dezimalzahl umwandeln.
Kennt ja jemand ne Lösung?
also 1101011010010 in irgend ne NORMAL zahl verwandeln!

jbg 3. Apr 2003 18:08

Delphi-Quellcode:
function BinToInt(const S: string): Integer;
var
  Index: Integer;
begin
  Result := 0;
  for Index := Length(s) downto 1 do
    if s[Index] = '1' then
      Result := Result or (1 shl (Length(s) - Index);
end;
(Normalerweise verwende ich i anstatt Index, aber das würde hier die Kursivschrift hervorrufen)

Duffy 3. Apr 2003 18:10

Hallo,
hier ist mal ein Lösungsansatz. Viel Spaß

bye

Delphi-Quellcode:
function NumBin(B: string): Longint;
var
  Accum, Power: Longint;
  P: Byte;
begin
  Power := 1;
  Accum := 0;
  for P := Length(B) downto 1 do
  begin
    If (B[P] = '0') or (B[P] = '1') then
    begin
      If B[P] = '1' then
      begin
        Inc(Accum, Power);
      end;
      Power := Power shl 1;
    end;
  end;
  NumBin := Accum;
end;


Alle Zeitangaben in WEZ +1. Es ist jetzt 23:19 Uhr.

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