Click here to Skip to main content
15,899,314 members
Home / Discussions / Visual Basic
   

Visual Basic

 
GeneralRe: Filesize Conversion Pin
The Man from U.N.C.L.E.5-May-10 6:39
The Man from U.N.C.L.E.5-May-10 6:39 
GeneralRe: Filesize Conversion Pin
Dayekh5-May-10 6:45
Dayekh5-May-10 6:45 
GeneralRe: Filesize Conversion Pin
riced5-May-10 8:34
riced5-May-10 8:34 
GeneralRe: Filesize Conversion Pin
Luc Pattyn5-May-10 8:37
sitebuilderLuc Pattyn5-May-10 8:37 
GeneralRe: Filesize Conversion Pin
riced5-May-10 9:54
riced5-May-10 9:54 
GeneralRe: Filesize Conversion Pin
Luc Pattyn5-May-10 10:33
sitebuilderLuc Pattyn5-May-10 10:33 
GeneralRe: Filesize Conversion Pin
riced5-May-10 10:49
riced5-May-10 10:49 
GeneralRe: Filesize Conversion Pin
Luc Pattyn5-May-10 12:20
sitebuilderLuc Pattyn5-May-10 12:20 
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.

Smile | :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]

Prolific encyclopedia fixture proof-reader browser patron addict?
We all depend on the beast below.


GeneralRe: Filesize Conversion Pin
riced5-May-10 22:09
riced5-May-10 22:09 
GeneralRe: Filesize Conversion Pin
Dayekh5-May-10 12:06
Dayekh5-May-10 12:06 
QuestionWeb Service Problems Pin
RobThompson5-May-10 0:25
RobThompson5-May-10 0:25 
AnswerRe: Web Service Problems Pin
Wayne Gaylard5-May-10 2:21
professionalWayne Gaylard5-May-10 2:21 
AnswerRe: Web Service Problems Pin
Dave Kreskowiak5-May-10 4:16
mveDave Kreskowiak5-May-10 4:16 
QuestionConvert into Char Pin
Anubhava Dimri4-May-10 19:11
Anubhava Dimri4-May-10 19:11 
AnswerRe: Convert into Char Pin
Richard MacCutchan4-May-10 21:39
mveRichard MacCutchan4-May-10 21:39 
GeneralRe: Convert into Char Pin
Anubhava Dimri4-May-10 21:57
Anubhava Dimri4-May-10 21:57 
GeneralRe: Convert into Char Pin
Richard MacCutchan4-May-10 22:09
mveRichard MacCutchan4-May-10 22:09 
GeneralRe: Convert into Char Pin
Anubhava Dimri4-May-10 22:22
Anubhava Dimri4-May-10 22:22 
GeneralRe: Convert into Char Pin
The Man from U.N.C.L.E.4-May-10 22:38
The Man from U.N.C.L.E.4-May-10 22:38 
GeneralRe: Convert into Char Pin
Richard MacCutchan5-May-10 0:44
mveRichard MacCutchan5-May-10 0:44 
GeneralRe: Convert into Char Pin
supercat96-May-10 7:02
supercat96-May-10 7:02 
GeneralRe: Convert into Char Pin
Anubhava Dimri6-May-10 18:34
Anubhava Dimri6-May-10 18:34 
AnswerRe: Convert into Char [modified] Pin
Estys4-May-10 23:22
Estys4-May-10 23:22 
QuestionMessage Removed Pin
4-May-10 13:11
gmhanna4-May-10 13:11 
AnswerRe: SQL Update "Update requires a valid update command" Pin
Johan Hakkesteegt4-May-10 23:43
Johan Hakkesteegt4-May-10 23:43 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.