![]() |
Problem mit Integer zu Binär
Moin,
mittels folgendem Code (Danke an Luckie :thumb: ) wandle ich Integerzahlen ins Binärformat um:
Code:
Es kunktioniert auch alles gut, nur leider steht dann bei der Ausgabe z.b. wenn ich 7 umrechne erhalte ich: 00000111. Wie kann ich aber die überflüssigen Nullen vor der eigentlichen Zahl löschen? Also das nur die "richtige" Zahl, in dem Falle 111 da steht ?
function IntToBin(Int: Integer): String;
var i : Integer; begin Result := ''; for i := 7 downto 0 do Result := Result + IntToStr((Int shr i) and 1); end; Vielen Dank, |
Re: Problem mit Integer zu Binär
|
Re: Problem mit Integer zu Binär
Ja, mit Pos und Delete hab ichs schonmal versucht, aber leider waren dann alle Nullen weg. Das Problem ist auch das, dass ich ja nicht weiß wann die "richtige" Zahl beginnt...
|
Re: Problem mit Integer zu Binär
Vielleicht geht's mit
Delphi-Quellcode:
S ist natürlich der String.
While Pos('0', s) = 1 do Delete(s, 1, 1)
|
Re: Problem mit Integer zu Binär
Mit dieser Funktion macht er keine Nullen voran:
Delphi-Quellcode:
function DezToBin(Zahl: Int64): String;
begin Result:=''; repeat if Zahl mod 2 =0 then begin Result := '0' + Result; end else begin Result := '1' + Result; end; Zahl := Zahl div 2; until Zahl = 0; end; |
Re: Problem mit Integer zu Binär
Code:
Hm, is das Integer64 ? Gibs das in D3 schon ??
(Zahl: Int64)
Mein D3 sagt undefinierter Bezeichner ! :( |
Re: Problem mit Integer zu Binär
Dann nimm doch einfach integer. ;)
|
Re: Problem mit Integer zu Binär
Ok, vielen Dank ! :thumb:
|
Re: Problem mit Integer zu Binär
Zitat:
|
Re: Problem mit Integer zu Binär
Hier was fixes...
glaub ich :zwinker:
Delphi-Quellcode:
Bye
Function IntToBinString( Const Value: Integer; Trim: Boolean = False ): String;
Var i: Byte; Begin SetLength( Result, 32 ); For i := 31 Downto 0 Do Begin If ( Value And ( $80000000 Shr ( 31 - i ) )Shr i ) = 1 Then Result[ 32 - i ] := '1' Else Result[ 32 - i ] := '0'; End; If Trim Then Result := Copy( Result, Pos( '1', Result ), Length( Result ) ); End; |
Alle Zeitangaben in WEZ +1. Es ist jetzt 09:43 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