-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathByteConverter.vb
40 lines (36 loc) · 1.2 KB
/
ByteConverter.vb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
Public Class ByteConverter
#Region " ~ String Array's ~ "
Dim binaryTypesArray As String() = {"KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"}
#End Region
#Region " ~ Enums ~ "
Enum BinaryTypes
KB = 0
MB = 1
GB = 2
TB = 3
PB = 4
EB = 5
ZB = 6
YB = 7
End Enum
#End Region
Public Function ConvertByte(ByVal bytes As Double) As String
Dim mString As String = ""
For i As Integer = 0 To binaryTypesArray.Length - 1
Dim index As Integer = (binaryTypesArray.Length - 1) - i
If bytes <= 1023 Then
mString = bytes & " bytes"
Exit For
ElseIf bytes >= 1024 ^ (index + 1) Then
mString = Math.Round(bytes / (1024 ^ (index + 1)), 2) & " " & binaryTypesArray(index)
Exit For
End If
Application.DoEvents()
Next
Return mString
End Function
Public Function ConvertByte(ByVal bytes As Double, ByVal outputBinaryType As BinaryTypes)
Dim index As Integer = outputBinaryType
Return Math.Round(bytes / (1024 ^ (index + 1)), 2) & " " & binaryTypesArray(index)
End Function
End Class