Results 1 to 6 of 6

Thread: Converting bytes to gigabytes.

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Aug 2013
    Posts
    25

    Question Converting bytes to gigabytes.

    Hello I am making a program in VB that grabs the system information, I want to get the amount of RAM installed, I currently have this code :

    Code:
      Dim CpuName As String
     Dim sysinfo As String
            CpuName = My.Computer.Registry.GetValue("HKEY_LOCAL_MACHINE\HARDWARE\DESCRIPTION\System\CentralProcessor\0", "ProcessorNameString", Nothing)
          
    
                    sysinfo = vbNewLine + "OS Name: " + My.Computer.Info.OSFullName + vbNewLine + "OS Version: " + My.Computer.Info.OSVersion + vbNewLine + "Computer Name: " + My.Computer.Name + vbNewLine + "CPU: " + CpuName + vbNewLine + "RAM: " + ((My.Computer.Info.TotalPhysicalMemory) / 1024 / 1024 / 1024).ToString
    
    RichTextBox1.Text = sysinfo
    However when I run it , I still get a huge number . I know that you should divide by 1024 , but im not sure what is the correct way to use it.

  2. #2
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: Converting bytes to gigabytes.

    This should do it
    Code:
    (My.Computer.Info.TotalPhysicalMemory / 1024 / 1024 / 1024).ToString("n2")
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Aug 2013
    Posts
    25

    Re: Converting bytes to gigabytes.

    thanks very much!

  4. #4
    Frenzied Member HanneSThEGreaT's Avatar
    Join Date
    Nov 2003
    Location
    Vereeniging, South Africa
    Posts
    1,492

    Re: Converting bytes to gigabytes.

    I was too late So I'll try to add something interesting as well

    Anyways, if you have a look at the next list :

    Code:
           '1 KB = 1024 - KiloByte
            '1 MB = 1024 ^ 2 - MegaByte
            '1 GB = 1024 ^ 3 - GigaByte
            '1 TB = 1024 ^ 4 - TeraByte
            '1 PB = 1024 ^ 5 - PetaByte
            '1 EB = 1024 ^ 6 - ExaByte
            '1 ZB = 1024 ^ 7 - ZettaByte
            '1 YB = 1024 ^ 8 - YottaByte
            '1 BB = 1024 ^ 9 - BrontoByte
    You will see that a GB is 1024 raised to the power of 3 - that is why you had to divide it three times. MB is 1024 ^ 2 ( so divide it two times ) TB is 1024 raised to the power of 4 - you'd then need to divide it four times.
    VB.NET MVP 2008 - Present

  5. #5
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,414

    Re: Converting bytes to gigabytes.

    another way to remember it:

    Code:
    Imports System.Numerics
    
    Public Class Form1
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Dim kb As New BigInteger(2 ^ 10) 'kb
            Dim Mb As New BigInteger(2 ^ 20) 'Mb
            Dim Gb As New BigInteger(2 ^ 30) 'Gb
            Dim Tb As New BigInteger(2 ^ 40) 'Tb
            Dim Pb As New BigInteger(2 ^ 50) 'Petabyte
            Dim Eb As New BigInteger(2 ^ 60) 'Exabyte
            Dim Zb As New BigInteger(2 ^ 70) 'Zettabyte
            Dim Yb As New BigInteger(2 ^ 80) 'Yottabyte
            Stop
        End Sub
    End Class

  6. #6
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: Converting bytes to gigabytes.

    Yes there are more general approaches. See the links to determine whether to use 1024 or 1000. In this case 1024 is appropriate because it is memory.
    Code:
        Public Enum Units 'Kfactor ^ units
            B = 0
            KB = 1 'kilo
            MB = 2 'mega
            GB = 3 'giga
            TB = 4 'tera
            PB = 5 'peta
            EB = 6 'exa
            ZB = 7 'zetta
            YB = 8 'yotta
            'add new values as needed
        End Enum
    
        'compute max value of enum
        Private ReadOnly maxU As Integer = [Enum].GetValues(GetType(Units)).Cast(Of Integer)().Max
    
        'kFactor should be set according to your use
        Private ReadOnly Kfactor As Integer = 1024 'or 1000
        'see
        'http://physics.nist.gov/cuu/Units/prefixes.html
        'and
        'http://physics.nist.gov/cuu/Units/binary.html
    
        Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
            Dim foo As Decimal = Decimal.MaxValue 'a large test number
            For Each unit As Units In [Enum].GetValues(GetType(Units))
                Dim conversionval As Decimal = CDec(Kfactor ^ unit)
                Dim fooconverted As Decimal = foo / conversionval
                Debug.WriteLine(String.Format("{0:n0} {1}   ({2:n0})", fooconverted, unit, conversionval))
            Next
        End Sub
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width