Format bytes into kB, MB, etc.

Share your advanced PureBasic knowledge/code with the community.
User avatar
Dreamland Fantasy
Enthusiast
Enthusiast
Posts: 335
Joined: Fri Jun 11, 2004 9:35 pm
Location: Glasgow, UK
Contact:

Format bytes into kB, MB, etc.

Post by Dreamland Fantasy »

Here is a routine that formats a given number of bytes into kB, MB, etc. It also has the option to use either JEDEC, IEC or metric formats.

Code: Select all

; Routine to format bytes into kB, MB, etc. Based on information at
; https://en.wikipedia.org/wiki/Mebibyte (retrieved 27th June 2018)
; 
; Programmed by Francis G. Loch, last updated 19th February 2024
;
; This is free and unencumbered software released into the public domain.
; 
; Anyone is free to copy, modify, publish, use, compile, sell, or
; distribute this software, either in source code form or as a compiled
; binary, for any purpose, commercial or non-commercial, and by any
; means.
; 
; In jurisdictions that recognize copyright laws, the author or authors
; of this software dedicate any and all copyright interest in the
; software to the public domain. We make this dedication for the benefit
; of the public at large and to the detriment of our heirs and
; successors. We intend this dedication to be an overt act of
; relinquishment in perpetuity of all present and future rights to this
; software under copyright law.
; 
; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
; EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
; MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
; IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
; OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
; ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
; OTHER DEALINGS IN THE SOFTWARE.
; 
; For more information, please refer to <http://unlicense.org>

Enumeration
  #FormatBytes_OSDefault
  #FormatBytes_JEDEC
  #FormatBytes_IEC
  #FormatBytes_Metric
EndEnumeration

Procedure.s FormatBytes(NbBytes.d, NbDecimals = #PB_Default, Mode = #FormatBytes_OSDefault)
  
  Protected Base, Exponent, MaxExponent, Unit$
  
  If NbDecimals = #PB_Default
    NbDecimals = 2
  EndIf
  
  If Mode = #FormatBytes_OSDefault
    CompilerIf #PB_Compiler_OS = #PB_OS_Windows
      Mode = #FormatBytes_JEDEC
    CompilerElseIf #PB_Compiler_OS = #PB_OS_Linux
      Mode = #FormatBytes_IEC
    CompilerElseIf #PB_Compiler_OS = #PB_OS_MacOS
      Mode = #FormatBytes_Metric
    CompilerEndIf
  EndIf
  
  Select Mode
    Case #FormatBytes_IEC
      Base = 1024
      Unit$ = "KiB,MiB,GiB,TiB,PiB,EiB,ZiB,YiB"
    Case #FormatBytes_JEDEC
      Base = 1024
      Unit$ = "KB,MB,GB"
    Case #FormatBytes_Metric
      Base = 1000
      Unit$ = "kB,MB,GB,TB,PB,EB,ZB,YB"
    Default
      ProcedureReturn "[ERROR] FormatBytes(): Invalid mode passed"
  EndSelect
  
  If NbBytes
    Exponent = Int(Log(NbBytes) / Log(Base))
  Else
    Exponent = 0
  EndIf
  MaxExponent = CountString(Unit$, ",") + 1
  If Exponent > MaxExponent
    Exponent = MaxExponent
  EndIf
  
  If Exponent
    ProcedureReturn FormatNumber(NbBytes / Pow(Base, Exponent), NbDecimals) + " " + StringField(Unit$, Exponent, ",")
  Else
    ProcedureReturn FormatNumber(NbBytes, 0) + " bytes"
  EndIf
  
EndProcedure

;- Test code below

n.d = Pow(1024, 4)

Debug FormatNumber(n, 0) + " bytes" + Chr(10)
Debug "JEDEC:" + Chr(9) + FormatBytes(n, #PB_Default, #FormatBytes_JEDEC)
Debug "IEC:" + Chr(9) + FormatBytes(n, #PB_Default, #FormatBytes_IEC)
Debug "Metric:" + Chr(9) + FormatBytes(n, #PB_Default, #FormatBytes_Metric)
If anyone has any improvements, please feel free to let me know. :)

Kind regards,

Francis
Last edited by Dreamland Fantasy on Mon Feb 19, 2024 1:53 am, edited 3 times in total.
User avatar
Dreamland Fantasy
Enthusiast
Enthusiast
Posts: 335
Joined: Fri Jun 11, 2004 9:35 pm
Location: Glasgow, UK
Contact:

Re: Format bytes into kB, MB, etc.

Post by Dreamland Fantasy »

The above code has been updated to include license information as per a request.

Kind regards,

Francis
jack
Addict
Addict
Posts: 1336
Joined: Fri Apr 25, 2003 11:10 pm

Re: Format bytes into kB, MB, etc.

Post by jack »

hello Dreamland Fantasy, I love your license :)
wish more people would use that license.
User avatar
Dreamland Fantasy
Enthusiast
Enthusiast
Posts: 335
Joined: Fri Jun 11, 2004 9:35 pm
Location: Glasgow, UK
Contact:

Re: Format bytes into kB, MB, etc.

Post by Dreamland Fantasy »

Hi there,

I have made an amendment to the above code as a bug was discovered when running it under PureBasic 6.10 beta 6 that didn't occur in previous versions.

The bug occurred at the exponent calculation stage if NbBytes is zero since log(0) is mathematically undefined.

Kind regards,

Francis
User avatar
Sicro
Enthusiast
Enthusiast
Posts: 538
Joined: Wed Jun 25, 2014 5:25 pm
Location: Germany
Contact:

Re: Format bytes into kB, MB, etc.

Post by Sicro »

Hello Dreamland Fantasy, thanks for the code/fix 8)

As soon as the final version of PB 6.10 has been released, I will also update the code in the archive:
PB-CodeArchive-Rebirth/String/FormatBytes.pbi
Image
Why OpenSource should have a license :: PB-CodeArchiv-Rebirth :: Pleasant-Dark (syntax color scheme) :: RegEx-Engine (compiles RegExes to NFA/DFA)
Manjaro Xfce x64 (Main system) :: Windows 10 Home (VirtualBox) :: Newest PureBasic version
Post Reply