Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Converting bytes into KB,MB,GB,TB,PB
#1
I needed a little function for convert bytes into KB,MB,GB,TB,PB.
Here she is..

Code: (Select All)
'Convert Bytes in KB,MB,GB,TB,TB
'put also spaces in long numbers

A$ = "123122555552344566": Print " "; CHIFFRESPACE$(A$)
A$ = "234855666": Print " "; CHIFFRESPACE$(A$)
A$ = "6000234855666": Print " "; CHIFFRESPACE$(A$)
A$ = "512445666": Print " "; CHIFFRESPACE$(A$)
A$ = "512": Print " "; CHIFFRESPACE$(A$)
A$ = "14265444444": Print " "; CHIFFRESPACE$(A$)
A$ = "1355": Print " "; CHIFFRESPACE$(A$)

Sleep
End




Function CHIFFRESPACE$ (D$)
    DD$ = D$: OCTEX$ = D$: DD1$ = "": sauto = 0
    For yu = Len(DD$) To 1 Step -1
        DD1$ = Mid$(DD$, yu, 1) + DD1$: sauto = sauto + 1: If sauto = 3 Then DD1$ = " " + DD1$: sauto = 0
    Next yu
    XCHIF$ = OCTEX$: YCHIF# = Val(XCHIF$)
    NBC = Int((Log(Val(XCHIF$)) / 2.303) + 1.01) 'number of digits... in OCTEX$
    If NBC < 4 Then FF$ = XCHIF$: UND$ = "bytes": GoTo aa
    If NBC < 7 Then ZCHIF# = Int(YCHIF# / 1024 * 10000) / 10000: FF$ = Str$(ZCHIF#): UND$ = "KB": GoTo aa
    If NBC < 10 Then ZCHIF# = Int(YCHIF# / 1024 / 1024 * 10000) / 10000: FF$ = Str$(ZCHIF#): UND$ = "MB": GoTo aa
    If NBC < 13 Then ZCHIF# = Int(YCHIF# / 1024 / 1024 / 1024 * 10000) / 10000: FF$ = Str$(ZCHIF#): UND$ = "GB": GoTo aa
    If NBC < 16 Then ZCHIF# = Int(YCHIF# / 1024 / 1024 / 1024 / 1024 * 10000) / 10000: FF$ = Str$(ZCHIF#): UND$ = "TB": GoTo aa
    ZCHIF# = Int(YCHIF# / 1024 / 1024 / 1024 / 1024 / 1024 * 10000) / 10000: FF$ = Str$(ZCHIF#): UND$ = "PB"
    aa: FF$ = _Trim$(FF$): If Left$(FF$, 1) = "." Then FF$ = "0" + FF$
    If InStr(FF$, ".") = 0 Then FF$ = FF$ + ".0000"
    JFF = InStr(FF$, "."): FF$ = Left$(FF$ + "0000000000000000", JFF + 4)
    JFF = InStr(FF$, "."): Mid$(FF$, JFF, 1) = ","
    Octey$ = "         (" + FF$ + " " + UND$ + ")"
    DD1$ = Left$(_Trim$(DD1$) + " bytes" + String$(60, 32), 55): W = Len(Octey$): Mid$(DD1$, Len(DD1$) - W, Len(Octey$)) = Octey$
    CHIFFRESPACE$ = DD1$

End Function
Why not yes ?
Reply
#2
Created this small function a long time ago:

Code: (Select All)
Print formatFileSize$(123122555552344566)
Print formatFileSize$(234855666)
Print formatFileSize$(6000234855666)
Print formatFileSize$(512445666)
Print formatFileSize$(512)
Print formatFileSize$(14265444444)
Print formatFileSize$(1355)

Function formatFileSize$ (ts As _Unsigned _Integer64)
t! = ts: i% = 0
Do While t! > 1024
i% = i% + 1: t! = t! / 1024
Loop
If i% > 0 Then
formatFileSize$ = _ToStr$(t!, 2 + Int(Log(t!) / Log(10))) + " " + Mid$("KMGTP", i%, 1) + "B"
Else
formatFileSize$ = _ToStr$(t!) + " B"
End If
End Function
45y and 2M lines of MBASIC>BASICA>QBASIC>QBX>QB64 experience
Reply
#3
Simply divide your known byte size by the "Binary size factor" constants available in each program since v4.0.0
Reply
#4
#mdijkens: Sure shorter. In fact, I cannot start your function.
Could you show here a full exemple with your function ?
Why not yes ?
Reply
#5
(05-30-2025, 03:08 PM)euklides Wrote: #mdijkens: Sure shorter. In fact, I cannot start this second function.
Could you show here a full exemple with your function ?
added in sample above
45y and 2M lines of MBASIC>BASICA>QBASIC>QBX>QB64 experience
Reply
#6
I can't access the page right now to copy the snippet but I prefer how I converted and displayed bytes as kilobytes, megabytes, gigabytes, and terabytes.
FTP Library
The noticing will continue
Reply
#7
Yes that is the whole point of the function
45y and 2M lines of MBASIC>BASICA>QBASIC>QBX>QB64 experience
Reply
#8
#mdijkens ! It is ok: I used an older version of QB64pe  where "_ToStr$() " was unknown !  Shy Thank's
Why not yes ?
Reply




Users browsing this thread: 1 Guest(s)