wakawaka

Data Unit Conversion (VBS)

Feb 15th, 2012
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. 'Example Usage
  2. Dim myConvertedUnit : Set myConvertedUnit = ConvertDataUnit(4096, DataUnit_Bytes, DataUnit_Auto, 2)
  3.  
  4. Msgbox myConvertedUnit.ToString, vbOkOnly, "Data Unit Usage Example"
  5.  
  6. '----------------------------------------------------------------------------------------------------
  7.  
  8. '''<summary>Defines constants for digital information size units as defined by the IEC.</summary>
  9. Public Const DataUnit_Auto = -1
  10. Public Const DataUnit_Bytes = 0
  11. Public Const DataUnit_Kibibytes = 1
  12. Public Const DataUnit_Mebibytes = 2
  13. Public Const DataUnit_Gibibytes = 3
  14. Public Const DataUnit_Tebibytes = 4
  15. Public Const DataUnit_Pebibytes = 5
  16. Public Const DataUnit_Exbibytes = 6
  17. Public Const DataUnit_Zebibytes = 7
  18. Public Const DataUnit_Yobibytes = 8
  19.  
  20.  
  21. '''<summary>Converts a value of DISU to another DISU</summary>
  22. '''<param name="dSize">The size of DISU</param>
  23. '''<param name="iCurrentUnit">The current DISU</param>
  24. '''<param name="iToUnit">The DISU to convert the size to</param>
  25. '''<param name="iPrecision">How many digits you want to the right of the decimal point after the conversion</param>
  26. '''<remarks>DISU stands for Digital Information Storage Unit</remarks>
  27. Public Function ConvertDataUnit(ByVal dSize, ByVal iCurrentUnit, ByVal iToUnit, ByVal iPrecision)
  28.     Dim dConvertedValue : dConvertedValue = 0
  29.     Dim iConvertedUnit : iConvertedUnit = 0
  30.     Dim dBaseBytes
  31.    
  32.     If iCurrentUnit >= 0 And iCurrentUnit <= 8 Then
  33.         dBaseBytes = dSize * (1024 ^ iCurrentUnit)
  34.        
  35.         Select Case True
  36.             Case iToUnit >= 0 And iToUnit <= 8
  37.                 dConvertedValue = Round(dBaseBytes / (1024 ^ iToUnit), iPrecision)
  38.                 iConvertedUnit = iToUnit
  39.             Case Else
  40.                 Do While dBaseBytes >= 1024
  41.                     dBaseBytes = dBaseBytes / 1024
  42.                     iConvertedUnit = iConvertedUnit + 1
  43.                 Loop
  44.                
  45.                 dConvertedValue = Round(dBaseBytes, iPrecision)
  46.         End Select
  47.        
  48.         Set ConvertDataUnit = New DataUnit.Init(dConvertedValue, iConvertedUnit)
  49.     Else
  50.         Err.Raise 5, "ConvertDataUnit(ByVal dSize, ByVal iCurrentUnit, ByVal iToUnit, ByVal iPrecision)", "An invalid unit of measure was given"
  51.     End If
  52. End Function
  53.  
  54.  
  55. '''<summary>Represents a Digital Information Storage Unit</summary>
  56. Class DataUnit
  57.     Private mValue
  58.     Private mUnit
  59.    
  60.     Public Property Get Value()
  61.         Value = mValue
  62.     End Property
  63.    
  64.     Public Property Let Value(ByVal dValue)
  65.         mValue = dValue
  66.     End Property
  67.    
  68.     Public Property Get Unit()
  69.         Unit = mUnit
  70.     End Property
  71.    
  72.     Public Property Let Unit(ByVal iUnit)
  73.         If iUnit < 0 Or iUnit > 8 Then
  74.             Err.Raise 5, "DataUnit.Unit(ByVal iUnit)", "An invalid unit of measure was given"
  75.             iUnit = 0
  76.         End If
  77.  
  78.         mUnit = iUnit
  79.     End Property
  80.    
  81.     Private Sub Class_Initialize()
  82.         mValue = 0
  83.         mUnit = 0
  84.     End Sub
  85.    
  86.     Public Function Init(ByVal dValue, ByVal iUnit)
  87.         mValue = dValue
  88.         Unit = iUnit
  89.        
  90.         Set Init = Me
  91.     End Function
  92.    
  93.     Public Function ToString()
  94.         Dim aUnitLabel : aUnitLabel = Array("Bytes", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB")
  95.        
  96.         ToString = mValue & " " & aUnitLabel(mUnit)
  97.     End Function
  98. End Class
Advertisement
Add Comment
Please, Sign In to add comment