Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. Filesize Conversion

Filesize Conversion

Scheduled Pinned Locked Moved Visual Basic
csharphelp
22 Posts 8 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Luc Pattyn

    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.


    R Offline
    R Offline
    riced
    wrote on last edited by
    #21

    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 = 0

      Do
         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.

    1 Reply Last reply
    0
    • L Lost User

      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.

      P Offline
      P Offline
      Paul Conrad
      wrote on last edited by
      #22

      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

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups