- Offizieller Beitrag
Habe es gerade gebraucht, vielleicht auch für euch nützlich. Z.B. beim Download mit InetGet zum Formatieren der übermittelten Anzahl gelesener Bytes.
AutoIt
Local Const $1TB = 0x10000000000
Local Const $1GB = 0x40000000
Local Const $1MB = 0x100000
Local Const $1KB = 0x400
Local $iByte = 2*$1TB + 355*$1GB + 1019*$1MB + 468*$1KB + 13
ConsoleWrite(_FormatByte($iByte) & @CRLF)
Local $tRet = _FormatByte($iByte, '', True)
ConsoleWrite( $iByte & ' Byte: ' & @LF & _
$tRet.TB & ' TB' & @LF & _
$tRet.GB & ' GB' & @LF & _
$tRet.MB & ' MB' & @LF & _
$tRet.KB & ' KB' & @LF & _
$tRet.Byte & ' Byte' & @LF)
; #FUNCTION# ====================================================================================================================
; Name ..........: _FormatByte
; Description ...: Formats a given value of bytes with highest or given unit, optional as structure with all units
; Parameters ....: $_iByte The value of bytes to format
; ...............: $_sUnit (Default = '', unit of highest value) or count of given unit (TB, GB, MB, KB, Byte)
; ...............: $_fStruct Returns a structure with .TB .GB .MB .KB .Byte (Default = False)
; Return values .: The formatted string or the structure.
; Author ........: BugFix
; ===============================================================================================================================
Func _FormatByte($_iByte, $_sUnit='', $_fStruct=False)
Local Static $aByte[5][2] = [[0x10000000000],[0x40000000],[0x100000],[0x400],[0x1]]
Local Static $tBytes = DllStructCreate('int TB;int GB;int MB;int KB;int Byte;')
Local Static $aUnit[5] = ['TB','GB','MB','KB','Byte']
Local $iModulo = $_iByte, $iHighest = 4
For $i = 0 To 3
$aByte[$i][1] = $iModulo >= $aByte[$i][0] ? Floor($iModulo/$aByte[$i][0]) : 0
$iModulo = $aByte[$i][1] > 0 ? Mod($iModulo,$aByte[$i][0]) : $iModulo
$iHighest = $aByte[$i][1] > 0 ? ($i < $iHighest ? $i : $iHighest) : $iHighest
Next
$aByte[4][1] = $iModulo
$tBytes.TB = $aByte[0][1]
$tBytes.GB = $aByte[1][1]
$tBytes.MB = $aByte[2][1]
$tBytes.KB = $aByte[3][1]
$tBytes.Byte = $aByte[4][1]
If $_fStruct Then Return $tBytes
$_sUnit = StringInStr('TB GB MB KB Byte', $_sUnit) ? $_sUnit : ''
$_sUnit = $_sUnit = '' ? $aUnit[$iHighest] : $_sUnit
Local $iUserUnit = Floor(StringInStr('TB GB MB KB Byte', $_sUnit)/3)
Return StringFormat('%.3f %s', $_iByte/$aByte[$iUserUnit][0], $aUnit[$iUserUnit])
EndFunc ;==>_FormatByte
Alles anzeigen
Edit: So, war ein Fehler in einem Statement. Nun sollte es wie gewünscht funktionieren.