Results 1 to 19 of 19

Thread: Converting bytes to kbytes.

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Sep 2000
    Location
    Atlanta
    Posts
    104

    Unhappy Converting bytes to kbytes.

    Can you believe I can't figure it out? And can you believe that I've never had to do this before? =).

    Anyway, what I need to do is convert an integer of a file's size from bytes to kbytes and mbytes etc.

    So say something is 1024 bytes, I know that's a kbyte, but what process / math do I have to use to work it all the way up to terabytes without loosing integrity in the numbers.

    Both to and from conversions would be useful.

    Thanks in advance. =).

  2. #2
    chenko
    Guest
    1024btyes = 1kbyte

    1024kbytes = 1mbyte

    1024mbytes = 1gbyte

    1024gbytes = 1tbyte

    simple eh?

  3. #3
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    and

    1 Exabyte = 1,152,921,504,606,846,976 Bytes
    = 1125899906842624 KBytes
    = 1099511627776 MBytes
    = 1073741824 GBytes
    = 1048576 TBytes
    = 1024 PetaBytes
    = 1 Exabyte

    Zettabyte ~ 1 000 000 000 000 000 000 000 bytes
    Yottabyte ~ 1 000 000 000 000 000 000 000 000 bytes



    And the reasons for the names :
    Kilo Greek khilioi = 1000
    Mega Greek megas = great, e.g., Alexandros Megos
    Giga Latin gigas = giant
    Tera Greek teras = monster
    Peta Greek pente = five, fifth prefix, peNta - N = peta
    Exa Greek hex = six, sixth prefix, Hexa - H = exa
    Remember, in standard French, the initial H is silent, so they would pronounce Hexa as Exa.
    Zetta almost homonymic with Greek Zeta, but last letter of the Latin alphabet
    Yotta almost homonymic with Greek iota, but penultimate letter of the Latin alphabet.
    Last edited by plenderj; Aug 2nd, 2001 at 04:32 AM.
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  4. #4
    -= B u g S l a y e r =- peet's Avatar
    Join Date
    Aug 2000
    Posts
    9,629
    VB Code:
    1. Option Explicit
    2.  
    3. Public Enum convTo
    4.     KB = 1
    5.     MB = 2
    6.     GB = 3
    7.     TB = 4
    8. End Enum
    9.  
    10. Public Function BytesTO(lBytes As Long, convertto As convTo) As Double
    11.     BytesTO = lBytes / (1024 ^ convertto)
    12. End Function
    13.  
    14. Private Sub Command1_Click()
    15.     MsgBox BytesTO(2048, KB)
    16. End Sub

    not sure abot this
    -= a peet post =-

  5. #5
    chenko
    Guest
    1024btyes = 1kbyte

    1024kbytes = 1mbyte

    1024mbytes = 1gbyte

    1024gbytes = 1tbyte

    simple eh?

    1mbytes = 1048576bytes
    1gbyte = 1,073,741,824bytes
    1tbyte = 1,099,511,627,776bytes

  6. #6
    chenko
    Guest
    Originally posted by peet
    VB Code:
    1. Option Explicit
    2.  
    3. Public Enum convTo
    4.     KB = 1
    5.     MB = 2
    6.     GB = 3
    7.     TB = 4
    8. End Enum
    9.  
    10. Public Function BytesTO(lBytes As Long, convertto As convTo) As Double
    11.     BytesTO = lBytes / (1024 ^ convertto)
    12. End Function
    13.  
    14. Private Sub Command1_Click()
    15.     MsgBox BytesTO(2048, KB)
    16. End Sub

    not sure abot this

    Option Explicit

    Public Enum convTo
    KB = 1
    MB = 2
    GB = 3
    TB = 4
    End Enum

    Public Function BytesTO(lBytes As Double, convertto As convTo) As Double
    BytesTO = lBytes / (1024 ^ convertto)
    End Function

    Private Sub Command1_Click()
    MsgBox BytesTO(1099511627776#, TB)
    End Sub


    Changed to As Double as there is a over flow on the 1TB, but it works.

  7. #7
    -= B u g S l a y e r =- peet's Avatar
    Join Date
    Aug 2000
    Posts
    9,629
    good

    maybe a bit overkill using enums and stuff, but if it works then ... I'm happy
    -= a peet post =-

  8. #8
    chenko
    Guest
    Migth also want to try lBytes As Currency to get large numbers

  9. #9

    Thread Starter
    Lively Member
    Join Date
    Sep 2000
    Location
    Atlanta
    Posts
    104
    Thanks for all the help. =).

    But I'm trying to do this in vbscript. If anyone knows another way to do it please reply.

    Thanks.

  10. #10
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    Peet's code should do it.
    Just dont declare the varibles as a specific type, and dont declare parameters as being a specific type either ;

    VB Code:
    1. Dim KB
    2. Dim MB
    3. Dim GB
    4. Dim TB
    5.  
    6. KB = 1
    7. MB = 2
    8. GB = 3
    9. TB = 4
    10.  
    11. Function BytesTO(lBytes, convertto)
    12.     BytesTO = lBytes / (1024 ^ convertto)
    13. End Function
    14.  
    15. Sub Command1_Click()
    16.     MsgBox BytesTO(2048, KB)
    17. End Sub

    That *should* work.
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  11. #11
    chenko
    Guest
    You would use Constants instead

    VB Code:
    1. Const KB = 1
    2. Const MB = 2
    3. Const GB = 3
    4. Const TB = 4
    5.  
    6. Function BytesTO(lBytes, convertto)
    7.     BytesTO = lBytes / (1024 ^ convertto)
    8. End Function
    9.  
    10. Response.Write(BytesTO(2048, KB))

  12. #12
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    I didnt think VBScript would support the Const keyword ... oh well, ya learn something new every day
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  13. #13
    Fanatic Member Bonker Gudd's Avatar
    Join Date
    Mar 2000
    Location
    Saturn
    Posts
    748
    Easy in C

    MB = kB >> 10


  14. #14
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    pffft C.
    Is that antequated piece of crap still around ?



    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  15. #15
    chenko
    Guest
    Originally posted by plenderj
    I didnt think VBScript would support the Const keyword ... oh well, ya learn something new every day
    And you dont use msgbox either or you dont really use Private Sub Command1_Click

  16. #16
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    Ah just hurry up and buy SBS2000 and then bugger off
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  17. #17
    chenko
    Guest
    hehe, Patience, Patience my son... you dont expect me to pay a few K in a day eh?

  18. #18
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    My other customers do
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  19. #19
    chenko
    Guest
    well so, we arnt in need of this, and it aint on our priorities yet

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