Page 1 of 1

Format bytes into kB, MB, etc.

Posted: Thu Jun 28, 2018 7:33 pm
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

Re: Format bytes into kB, MB, etc.

Posted: Sun Jul 01, 2018 12:58 pm
by Dreamland Fantasy
The above code has been updated to include license information as per a request.

Kind regards,

Francis

Re: Format bytes into kB, MB, etc.

Posted: Sun Jul 01, 2018 4:54 pm
by jack
hello Dreamland Fantasy, I love your license :)
wish more people would use that license.

Re: Format bytes into kB, MB, etc.

Posted: Mon Feb 19, 2024 1:26 am
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

Re: Format bytes into kB, MB, etc.

Posted: Mon Feb 19, 2024 7:54 pm
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