![]() |
Byte-Werte zwischen den Einheiten umrechnen
Da ich hier neu bin will ich mal einen kleinen Beitrag leisten und eine von mir geschriebene Funktion hier zur Verfügung stellen:
Delphi-Quellcode:
Beschreibung
function ConvertByte(BytesCount:Int64; Short:Boolean):String;
//BytesCount: Anzahl der Bytes //Short: Falls TRUE wird die Einheit in Kurzform, sonst in voller Schreibweise zurückgegeben. const ByteShortUnits:array[0..8] of String = ('B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'); ByteLongUnits:array[0..8] of String = ('Byte', 'KiloByte', 'MegaByte', 'GigaByte', 'TeraByte', 'PetaByte', 'ExaByte', 'ZettaByte', 'YottaByte'); var cc:Integer; OutputUnit:String; begin for cc:=1 to Length(ByteShortUnits) - 1 do if Power(2, cc * 10) > BytesCount then Break; Dec(cc); if Short then OutputUnit:=ByteShortUnits[cc] else OutputUnit:=ByteLongUnits[cc]; Result:=FloatToStr(RoundTo(BytesCount / Power(2, cc * 10), -2)) + ' ' + OutputUnit; end; Liefert aus Byte-Anzahl die nächstpassende Byte-Einheit als String. Die Einheit kann in Kurzform (z.B. Byte > B, KiloByte > KB) oder in voller Form ausgegeben werden. Die Funktion sollte eigentlich mit jeder Delphi-Version funktionieren. Habe jedoch nur in Delphi 7 getestet. Beispiele
Delphi-Quellcode:
Benötigte Units
ShowMessage(ConvertByte(1023, TRUE)); //Liefert: "1023 B"
ShowMessage(ConvertByte(1024, TRUE)); //Liefert: "1 KB" ShowMessage(ConvertByte(1023, FALSE)); //Liefert: "1023 Byte" ShowMessage(ConvertByte(1024, FALSE)); //Liefert: "1 KiloByte" ShowMessage(ConvertByte(3145728, TRUE)); //Liefert: "3 MB" ShowMessage(ConvertByte(3145728, FALSE)); //Liefert: "3 MegaByte"
Khabarakh hat auf Basis ![]()
Delphi-Quellcode:
mfg
function FileSizeToStr(const ASize: Int64 {für Delphiversionen < 2005 durch Real/Double/Extended ersetzen}; const AUseShortNames: Boolean = true): string;
const ShortUnits: Array[0..8] of string = ('Byte', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'); LongUnits: Array[0..8] of string = ('B', 'Kilob', 'Megab', 'Gigab', 'Terab', 'Petab', 'Exab', 'Zettab', 'Yottab'); Epsilon = 1 / 1024; // Wegen der Fließkommaungenauigkeit sicher nie falsch, verhindert außerdem Anzeigen wie "1024 KB" var Index: Integer; begin Index := Trunc(ln(ASize) / ln(2) / 10 + Epsilon); if AUseShortNames then Result := Format('%.2f %s', [ASize / (Int64(1) shl (Index * 10)), ShortUnits[Index]]) else Result := Format('%.2f %s%s', [ASize / (Int64(1) shl (Index * 10)), LongUnits[Index], 'yte']) end; [edit=Chakotay1308]Khabarakh's Code angefügt. Mfg, Chakotay1308[/edit] [edit=Chakotay1308]Titel angepasst. Mfg, Chakotay1308[/edit] |
Alle Zeitangaben in WEZ +1. Es ist jetzt 13:11 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