Filesize Conversion
-
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.
-
Thank you all for your input. I have finally written a function to do exactly what I need to be done. Please look at the code below and benefit from it if you wish:
Public Function BytesFormatting(ByVal Bytes As Integer) As String Dim FormattedFileSize As Double If Bytes < 1024 Then ''code to format fsize as KB Dim dblFormatted As Double dblFormatted = Bytes FormattedFileSize = Format(dblFormatted, "###,###.00") + "KB" ElseIf (Bytes > 1024 And Bytes < 1048576) Then ''code to format fsize as KB Dim dblFormatted As Double dblFormatted = (Bytes + 1023) / 1024 FormattedFileSize = Format(dblFormatted, "###,###.00") + "KB" ElseIf Bytes > 1048576 Then ''code to format fsize as MB Dim dblFormatted As Double dblFormatted = (Bytes / 1024) / 1024 FormattedFileSize = Format(dblFormatted, "###,###,###.00") + "MB" End If Return FormattedFileSize.ToString End Function
What if file is exactly 1024 or 1048576 bytes? Not likely but not impossible, I can generate one of either size with ease. :)
Regards David R --------------------------------------------------------------- "Every program eventually becomes rococo, and then rubble." - Alan Perlis The only valid measurement of code quality: WTFs/minute.
-
What if file is exactly 1024 or 1048576 bytes? Not likely but not impossible, I can generate one of either size with ease. :)
Regards David R --------------------------------------------------------------- "Every program eventually becomes rococo, and then rubble." - Alan Perlis The only valid measurement of code quality: WTFs/minute.
right. and doesn't
Dim FormattedFileSize As Double
deserve some attention too? :)Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
Prolific encyclopedia fixture proof-reader browser patron addict?
We all depend on the beast below.
-
right. and doesn't
Dim FormattedFileSize As Double
deserve some attention too? :)Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
Prolific encyclopedia fixture proof-reader browser patron addict?
We all depend on the beast below.
Yup. But I guess he has Option Strict Off either intentionally or by default. That allows it to compile. But there's even worse when you look at it more closely e.g. a file of 1023 bytes returns 1,023.00KB. :-D He did say it was working so I suppose he tested it. :laugh:
Regards David R --------------------------------------------------------------- "Every program eventually becomes rococo, and then rubble." - Alan Perlis The only valid measurement of code quality: WTFs/minute.
-
Yup. But I guess he has Option Strict Off either intentionally or by default. That allows it to compile. But there's even worse when you look at it more closely e.g. a file of 1023 bytes returns 1,023.00KB. :-D He did say it was working so I suppose he tested it. :laugh:
Regards David R --------------------------------------------------------------- "Every program eventually becomes rococo, and then rubble." - Alan Perlis The only valid measurement of code quality: WTFs/minute.
I have finally written a function to do exactly what I need to be done. Please look at the code below and benefit from it if you wish I think he meant: no need for me to test it, just publish and wait a while. Not sure it will still do what "needs to be done" when we are through... :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
Prolific encyclopedia fixture proof-reader browser patron addict?
We all depend on the beast below.
-
Thank you all for your input. I have finally written a function to do exactly what I need to be done. Please look at the code below and benefit from it if you wish:
Public Function BytesFormatting(ByVal Bytes As Integer) As String Dim FormattedFileSize As Double If Bytes < 1024 Then ''code to format fsize as KB Dim dblFormatted As Double dblFormatted = Bytes FormattedFileSize = Format(dblFormatted, "###,###.00") + "KB" ElseIf (Bytes > 1024 And Bytes < 1048576) Then ''code to format fsize as KB Dim dblFormatted As Double dblFormatted = (Bytes + 1023) / 1024 FormattedFileSize = Format(dblFormatted, "###,###.00") + "KB" ElseIf Bytes > 1048576 Then ''code to format fsize as MB Dim dblFormatted As Double dblFormatted = (Bytes / 1024) / 1024 FormattedFileSize = Format(dblFormatted, "###,###,###.00") + "MB" End If Return FormattedFileSize.ToString End Function
I decide to benefit and wrote something that does work. :-D Try plugging yours into the Main and see what happens. :)
Module Module1
Sub Main()
Dim s As String = ""
Console.WriteLine("--- Kilobytes ---")
s = FormattedFileSize(1023)
Console.WriteLine("1023 > " & s)
s = FormattedFileSize(1024)
Console.WriteLine("1024 > " & s)
s = FormattedFileSize(1025)
Console.WriteLine("1025 > " & s)Console.WriteLine("--- Megabytes ---") s = FormattedFileSize(1048575) Console.WriteLine("1048575 > " & s) s = FormattedFileSize(1048576) Console.WriteLine("1048576 > " & s) s = FormattedFileSize(1048577) Console.WriteLine("1048577 > " & s) s = FormattedFileSize(1048576 \* 2) Console.WriteLine("1048577 \* 2 > " & s) Console.WriteLine("--- Gigabytes ---") s = FormattedFileSize(1048576 \* 1024) Console.WriteLine("1048576 \* 1024 > " & s) Console.ReadLine()
End Sub
Public Function FormattedFileSize(ByVal sizeInBytes As Long) As String
Dim suffix() As String = New String() {"", "KB", "MB", "GB", "XX"}
Dim retVal As String = "Oops"
Dim units As Double = sizeInBytes
Dim index As Integer = 0
Do
units = units / 1024.0
index += 1
Loop While units >= 1024.0retVal = Format(units, "###,###,##0.000") + suffix(index) Return retVal
End Function
End Module
PS you just need to change the name of the function calls e.g. use
s = BytesFormatting(1024)
Regards David R --------------------------------------------------------------- "Every program eventually becomes rococo, and then rubble." - Alan Perlis The only valid measurement of code quality: WTFs/minute.
-
Thank you all for your input. I have finally written a function to do exactly what I need to be done. Please look at the code below and benefit from it if you wish:
Public Function BytesFormatting(ByVal Bytes As Integer) As String Dim FormattedFileSize As Double If Bytes < 1024 Then ''code to format fsize as KB Dim dblFormatted As Double dblFormatted = Bytes FormattedFileSize = Format(dblFormatted, "###,###.00") + "KB" ElseIf (Bytes > 1024 And Bytes < 1048576) Then ''code to format fsize as KB Dim dblFormatted As Double dblFormatted = (Bytes + 1023) / 1024 FormattedFileSize = Format(dblFormatted, "###,###.00") + "KB" ElseIf Bytes > 1048576 Then ''code to format fsize as MB Dim dblFormatted As Double dblFormatted = (Bytes / 1024) / 1024 FormattedFileSize = Format(dblFormatted, "###,###,###.00") + "MB" End If Return FormattedFileSize.ToString End Function
Thank you all for your input! I certainly was happy that this issue had a lot of interest. "i would hope that anybody who is working in IT knows the relationships between bits bytes k, m, g, t.....and on and on." -- knowing the relationship between bytes, KB, MB, etc.. was irrelevant as I was asking if there was a class/method that automatically did the conversions in VB.Net. And yes, I know the relationship between them :P "I think he meant: no need for me to test it, just publish and wait a while." -- Yea I'm sure all programmers are perfect testers of THEIR own code. I did some testing, but it obviously wasn't good enough. I suppose that's what learning is.. no? "What if file is exactly 1024 or 1048576 bytes?" -- Good point! I was swamped with work that I overlooked that fault. Thank you. "But there's even worse when you look at it more closely e.g. a file of 1023 bytes returns 1,023.00KB." -- Another good point. I need to address all eventualities more closely! "I decide to benefit and wrote something that does work." -- Thank you. I will work through that code. Thanks again all! This made my day.
-
I decide to benefit and wrote something that does work. :-D Try plugging yours into the Main and see what happens. :)
Module Module1
Sub Main()
Dim s As String = ""
Console.WriteLine("--- Kilobytes ---")
s = FormattedFileSize(1023)
Console.WriteLine("1023 > " & s)
s = FormattedFileSize(1024)
Console.WriteLine("1024 > " & s)
s = FormattedFileSize(1025)
Console.WriteLine("1025 > " & s)Console.WriteLine("--- Megabytes ---") s = FormattedFileSize(1048575) Console.WriteLine("1048575 > " & s) s = FormattedFileSize(1048576) Console.WriteLine("1048576 > " & s) s = FormattedFileSize(1048577) Console.WriteLine("1048577 > " & s) s = FormattedFileSize(1048576 \* 2) Console.WriteLine("1048577 \* 2 > " & s) Console.WriteLine("--- Gigabytes ---") s = FormattedFileSize(1048576 \* 1024) Console.WriteLine("1048576 \* 1024 > " & s) Console.ReadLine()
End Sub
Public Function FormattedFileSize(ByVal sizeInBytes As Long) As String
Dim suffix() As String = New String() {"", "KB", "MB", "GB", "XX"}
Dim retVal As String = "Oops"
Dim units As Double = sizeInBytes
Dim index As Integer = 0
Do
units = units / 1024.0
index += 1
Loop While units >= 1024.0retVal = Format(units, "###,###,##0.000") + suffix(index) Return retVal
End Function
End Module
PS you just need to change the name of the function calls e.g. use
s = BytesFormatting(1024)
Regards David R --------------------------------------------------------------- "Every program eventually becomes rococo, and then rubble." - Alan Perlis The only valid measurement of code quality: WTFs/minute.
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.
-
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