Filesize Conversion
-
You have some IndexOutOfRange problems... As a little VB exercise, here an alternative, spanning a larger range of values; the result is slightly different, notation is float or integer as appropriate:
Private Sub Button1\_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim pow As Long = 1 For i = 0 To 63 Dim fraction As Long = pow \\ 32 If fraction < 1 Then fraction = 1 test(-pow) test(pow - fraction) test(pow) test(pow + fraction) If pow >= &H4000000000000000 Then Exit For pow = 2 \* pow Next End Sub Public Sub test(ByVal value As Long) Dim s As String s = FormattedFileSize(value) Console.WriteLine(value.ToString().PadLeft(20) & " = " & value.ToString("X16") & " = " & s & "B") End Sub Public Function FormattedFileSize(ByVal sizeInBytes As Long) As String Dim negative As Boolean = False Dim result As String Dim value As Long = sizeInBytes Dim remainder As Long = 0 Dim suffixIndex As Integer If value < 0 Then negative = True value = -value End If For suffixIndex = 0 To 20 If value <= 1023 Then Exit For remainder = value Mod 1024 value = value \\ 1024 Next If remainder > 0 Then Dim d As Double = value + remainder / 1024 result = d.ToString("F3") Else result = value.ToString() End If If suffixIndex > 0 Then result = result & " KMGTPEZY"(suffixIndex) If negative Then result = "-" & result Return result End Function
Homework: find and fix the value(s) that still fail. :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
Prolific encyclopedia fixture proof-reader browser patron addict?
We all depend on the beast below.
I knew it would error for extremely large sizes and failed on negatives so here's a fix. (Not sure the negatives a right.)
Public Function FormattedFileSize(ByVal sizeInBytes As Long) As String
Dim suffix() As String = New String() {"Oops", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB", "XX"}
Dim units As Double = Math.Abs(sizeInBytes) 'Fudge to deal with negatives. Should it error with neg file size?
Dim index As Integer = 0Do units = units / 1024.0 index += 1 Loop While units >= 1024.0 Return Format(units, "###,###,##0.000") + suffix(index)
End Function
I've not done homework for years. :)
Regards David R --------------------------------------------------------------- "Every program eventually becomes rococo, and then rubble." - Alan Perlis The only valid measurement of code quality: WTFs/minute.
-
daveauld wrote:
i would hope that anybody who is working in IT knows the relationships between bits bytes k, m, g, t.....and on and on.
Never assume this. I once heard one of our IT help desk reps ask a colleague "What exactly is hexadecimal?".
It's time for a new signature.
Richard MacCutchan wrote:
once heard one of our IT help desk reps ask a colleague "What exactly is hexadecimal?"
:omg:
"The clue train passed his station without stopping." - John Simmons / outlaw programmer "Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon "Not only do you continue to babble nonsense, you can't even correctly remember the nonsense you babbled just minutes ago." - Rob Graham