VB.NET || How To Convert Bytes To Kilobytes, Megabytes, Gigabytes, Terabytes Using VB.NET
The following is a module with functions which demonstrates how to convert bytes to decimal formats like kilobytes, megabytes, gigabytes, terabytes, petabytes, exabytes, zettabytes, and yottabytes, as well as binary formats like kibibytes, mebibytes, gibibytes, tebibytes, pebibytes, exbibytes, zebibytes, and yobibytes using VB.NET.
The function demonstrated on this page follows the IEC standard, which means:
• 1 kilobyte = 1000 bytes (Decimal)
• 1 kibibyte = 1024 bytes (Binary)
This function allows you to convert bytes to a measurement unit, a measurement unit to bytes, and allows to convert from one measurement unit to another measurement unit.
1. Convert Bytes To Measurement Unit
The example below demonstrates the use of ‘Utils.Bytes.FromTo‘ to convert bytes to a measurement unit.
The optional function parameter allows you to specify the decimal places.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
' Convert Bytes To Measurement Unit ' Declare values to convert Dim values = {2287879, 536870912, 1073741824} ' Convert values For Each value In values ' Convert bytes to megabyte Dim mb = Utils.Bytes.FromTo(Utils.Bytes.Unit.Byte, value, Utils.Bytes.Unit.Megabyte) ' Convert bytes to mebibyte Dim mib = Utils.Bytes.FromTo(Utils.Bytes.Unit.Byte, value, Utils.Bytes.Unit.Mebibyte) ' Display the converted values Debug.Print($"Bytes: {value}, Megabyte: {mb}, Mebibyte: {mib}") Next ' expected output: ' Bytes: 2287879, Megabyte: 2.287879, Mebibyte: 2.18189144134521484375 ' Bytes: 536870912, Megabyte: 536.870912, Mebibyte: 512 ' Bytes: 1073741824, Megabyte: 1073.741824, Mebibyte: 1024 |
2. Convert Measurement Unit To Bytes
The example below demonstrates the use of ‘Utils.Bytes.FromTo‘ to convert a measurement unit to bytes.
The optional function parameter allows you to specify the decimal places.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
' Convert Measurement Unit To Bytes ' Declare values to convert Dim values = {1, 0.5D, 10.75D} ' Convert values For Each value In values ' Convert gibibyte to byte Dim bytes = Utils.Bytes.FromTo(Utils.Bytes.Unit.Gibibyte, value, Utils.Bytes.Unit.Byte) ' Display the converted values Debug.Print($"Gibibyte: {value}, Bytes: {bytes}") Next ' expected output: ' Gibibyte: 1, Bytes: 1073741824 ' Gibibyte: 0.5, Bytes: 536870912.0 ' Gibibyte: 10.75, Bytes: 11542724608.00 |
3. Convert Measurement Unit To Measurement Unit
The example below demonstrates the use of ‘Utils.Bytes.FromTo‘ to convert a measurement unit to another measurement unit.
The optional function parameter allows you to specify the decimal places.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
' Convert Measurement Unit To Measurement Unit ' Declare values to convert Dim values = {1991, 1987, 28.31D, 19.22D} ' Convert values For Each value In values ' Convert value from one unit to another Dim size = Utils.Bytes.FromTo(Utils.Bytes.Unit.Gigabyte, value, Utils.Bytes.Unit.Terabyte) ' Display the converted values Debug.Print($"Gigabyte: {value}, Terabyte: {size}") Next ' expected output: ' Gigabyte: 1991, Terabyte: 1.991 ' Gigabyte: 1987, Terabyte: 1.987 ' Gigabyte: 28.31, Terabyte: 0.02831 ' Gigabyte: 19.22, Terabyte: 0.01922 |
4. Utils Namespace
The following is the Utils Namespace. Include this in your project to start using!
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 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 |
' ============================================================================ ' Author: Kenneth Perkins ' Date: Dec 3, 2020 ' Taken From: http://programmingnotes.org/ ' File: Utils.vb ' Description: Handles general utility functions ' ============================================================================ Option Strict On Option Explicit On Namespace Global.Utils Namespace Bytes Public Module modBytes Public Enum Unit [Byte] ' Decimal Kilobyte Megabyte Gigabyte Terabyte Petabyte Exabyte Zettabyte Yottabyte ' Binary Kibibyte Mebibyte Gibibyte Tebibyte Pebibyte Exbibyte Zebibyte Yobibyte End Enum ''' <summary> ''' Converts a measurement 'Unit' to another measurement 'Unit' ''' </summary> ''' <param name="unitFrom">The measurement unit converting from</param> ''' <param name="sizeFrom">The size of the 'from' measurement unit</param> ''' <param name="unitTo">The measurement unit to convert to</param> ''' <param name="decimalPlaces">The decimal places to round to</param> ''' <returns>The value converted to the specified measurement unit</returns> Public Function FromTo(unitFrom As Unit, sizeFrom As Decimal, unitTo As Unit _ , Optional decimalPlaces As Integer? = Nothing) As Decimal Dim result = sizeFrom If unitFrom <> unitTo Then If unitFrom = Unit.Byte Then result = ConvertTo(unitTo, sizeFrom, decimalPlaces) ElseIf unitTo = Unit.Byte Then result = ConvertFrom(unitFrom, sizeFrom, decimalPlaces) Else result = ConvertTo(unitTo, ConvertFrom(unitFrom, sizeFrom), decimalPlaces) End If End If Return result End Function Private Enum Conversion From [To] End Enum ' Converts bytes to a measurement unit Private Function ConvertTo(unit As Unit, bytes As Decimal _ , Optional decimalPlaces As Integer? = Nothing) As Decimal Return Convert(Conversion.To, bytes, unit, decimalPlaces) End Function ' Converts a measurement unit to bytes Private Function ConvertFrom(unit As Unit, bytes As Decimal _ , Optional decimalPlaces As Integer? = Nothing) As Decimal Return Convert(Conversion.From, bytes, unit, decimalPlaces) End Function Private Function Convert(operation As Conversion, bytes As Decimal, unit As Unit _ , decimalPlaces As Integer?) As Decimal ' Get the unit type definition Dim definition = GetDefinition(unit) If definition Is Nothing Then Throw New ArgumentException($"Unknown unit type: {unit}", NameOf(unit)) End If ' Get the unit value Dim value = definition.Value ' Calculate the result Dim result = If(operation = Conversion.To, bytes / value, bytes * value) If decimalPlaces.HasValue Then result = Math.Round(result, decimalPlaces.Value, MidpointRounding.AwayFromZero) End If Return result End Function Public Enum Prefix [Decimal] Binary End Enum Public Class Definition Public Property Prefix As Prefix Public Property OrderOfMagnitude As Integer Public ReadOnly Property Multiple As Decimal Get Return If(Prefix = Prefix.Decimal, 1000, 1024) End Get End Property Public ReadOnly Property Value As Decimal Get Return CDec(Math.Pow(Multiple, OrderOfMagnitude)) End Get End Property End Class Public Function GetDefinition(unit As Unit) As Definition Dim definitions = GetDefinitions() Return If(definitions.ContainsKey(unit), definitions(unit), Nothing) End Function Public Function GetDefinitions() As Dictionary(Of Unit, Definition) ' Create and add definitions Static definitions As Dictionary(Of Unit, Definition) If definitions Is Nothing Then definitions = New Dictionary(Of Unit, Definition) ' Place units in order of magnitude ' Decimal units Dim decimals = { Unit.Kilobyte, Unit.Megabyte, Unit.Gigabyte, Unit.Terabyte _ , Unit.Petabyte, Unit.Exabyte, Unit.Zettabyte, Unit.Yottabyte } ' Binary units Dim binary = { Unit.Kibibyte, Unit.Mebibyte, Unit.Gibibyte, Unit.Tebibyte _ , Unit.Pebibyte, Unit.Exbibyte, Unit.Zebibyte, Unit.Yobibyte } AddDefinitions(definitions, Prefix.Decimal, decimals) AddDefinitions(definitions, Prefix.Binary, binary) End If Return definitions End Function Private Sub AddDefinitions(definitions As Dictionary(Of Unit, Definition) _ , prefix As Prefix, units As IEnumerable(Of Unit)) For index = 0 To units.Count - 1 Dim unit = units(index) If Not definitions.ContainsKey(unit) Then definitions.Add(unit, New Definition With { .Prefix = prefix, .OrderOfMagnitude = index + 1 }) End If Next End Sub End Module End Namespace End Namespace ' http://programmingnotes.org/ |
5. More Examples
Below are more examples demonstrating the use of the ‘Utils‘ Namespace. Don’t forget to include the module when running the examples!
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 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
' ============================================================================ ' Author: Kenneth Perkins ' Date: Dec 3, 2020 ' Taken From: http://programmingnotes.org/ ' File: Program.vb ' Description: The following demonstrates the use of the Utils Namespace ' ============================================================================ Option Strict On Option Explicit On Imports System Public Module Program Sub Main(args As String()) Try ' Declare values to convert Dim values = {2287879, 536870912, 1073741824} ' Convert values For Each value In values ' Convert bytes to megabyte Dim mb = Utils.Bytes.FromTo(Utils.Bytes.Unit.Byte, value, Utils.Bytes.Unit.Megabyte) ' Convert bytes to mebibyte Dim mib = Utils.Bytes.FromTo(Utils.Bytes.Unit.Byte, value, Utils.Bytes.Unit.Mebibyte) ' Display the converted values Display($"Bytes: {value}, Megabyte: {mb}, Mebibyte: {mib}") Next Display("") ' Declare values to convert Dim values2 = {1, 0.5D, 10.75D} ' Convert values For Each value In values2 ' Convert gibibyte to byte Dim bytes = Utils.Bytes.FromTo(Utils.Bytes.Unit.Gibibyte, value, Utils.Bytes.Unit.Byte) ' Display the converted values Display($"Gibibyte: {value}, Bytes: {bytes}") Next Display("") ' Declare values to convert Dim values3 = {1991, 1987, 28.31D, 19.22D} ' Convert values For Each value In values3 ' Convert value from one unit to another Dim size = Utils.Bytes.FromTo(Utils.Bytes.Unit.Gigabyte, value, Utils.Bytes.Unit.Terabyte) ' Display the converted values Display($"Gigabyte: {value}, Terabyte: {size}") Next Catch ex As Exception Display(ex.ToString) Finally Console.ReadLine() End Try End Sub Public Sub Display(message As String) Console.WriteLine(message) Debug.Print(message) End Sub End Module ' http://programmingnotes.org/ |
QUICK NOTES:
The highlighted lines are sections of interest to look out for.
The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.
Leave a Reply