Correct filesize calculation

Everything else that doesn't fall into one of the other PB categories.
User avatar
Fluid Byte
Addict
Addict
Posts: 2336
Joined: Fri Jul 21, 2006 4:41 am
Location: Berlin, Germany

Correct filesize calculation

Post by Fluid Byte »

I was always mistaken that 1 KB consists of 1000 Bytes. But actually it's 1024. So I'm woundering how to correctly calculate any type of size (KB, MB , GB, etc.)

Is that correct?

1024 Bytes = 1 Kilo Byte
1024 ^ 2 = 1 MB ?
1024 ^ 3 = 1 GB ?
Windows 10 Pro, 64-Bit / Whose Hoff is it anyway?
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post by Trond »

User avatar
Fluid Byte
Addict
Addict
Posts: 2336
Joined: Fri Jul 21, 2006 4:41 am
Location: Berlin, Germany

Post by Fluid Byte »

The heck.........

Google does math? Nice! 8)

Thanks for clearing up Trond.
Windows 10 Pro, 64-Bit / Whose Hoff is it anyway?
User avatar
Fluid Byte
Addict
Addict
Posts: 2336
Joined: Fri Jul 21, 2006 4:41 am
Location: Berlin, Germany

Post by Fluid Byte »

I have put the filesize calculation into executable code now. Hope this is correct, isn't it?

Code: Select all

#KILOBYTE = 1024
#MEGABYTE = 1024 * #KILOBYTE
#GIGABYTE = 1024 * #MEGABYTE
#TERABYTE = 1024 * #GIGABYTE

Filename$ = OpenFileRequester("","","All Files (*.*)|*.*",0)

If Filename$
	Size = FileSize(Filename$)
	
	If Size < 1024 : Size$ = Str(Size) + " Byte" : EndIf				
	If Size >= 1024 And Size < #MEGABYTE : Size$ = StrF(Size/1000,2) + " KB" : EndIf
	If Size >= #MEGABYTE And Size < #GIGABYTE : Size$ = StrF(Size/1000000,2) + " MB" : EndIf
	
	Debug "Filesize: " + Size$
EndIf
Windows 10 Pro, 64-Bit / Whose Hoff is it anyway?
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post by Trond »

Without looking at the code logic I'd recommend you to use StrD() instead of StrF().
Pupil
Enthusiast
Enthusiast
Posts: 715
Joined: Fri Apr 25, 2003 3:56 pm

Post by Pupil »

Fluid Byte wrote:I have put the filesize calculation into executable code now. Hope this is correct, isn't it?

Code: Select all

#KILOBYTE = 1024
#MEGABYTE = 1024 * #KILOBYTE
#GIGABYTE = 1024 * #MEGABYTE
#TERABYTE = 1024 * #GIGABYTE

Filename$ = OpenFileRequester("","","All Files (*.*)|*.*",0)

If Filename$
	Size = FileSize(Filename$)
	
	If Size < 1024 : Size$ = Str(Size) + " Byte" : EndIf				
	If Size >= 1024 And Size < #MEGABYTE : Size$ = StrF(Size/1000,2) + " KB" : EndIf
	If Size >= #MEGABYTE And Size < #GIGABYTE : Size$ = StrF(Size/1000000,2) + " MB" : EndIf
	
	Debug "Filesize: " + Size$
EndIf
The intervals are correct but calculations are wrong.. you should divide by 1024^x to get the right values :)
User avatar
Flype
Addict
Addict
Posts: 1542
Joined: Tue Jul 22, 2003 5:02 pm
Location: In a long distant galaxy

Post by Flype »

you might be interested or just want to compare results with this Win32 snippet :

Code: Select all

Enumeration 0
  #STIF_DEFAULT
  #STIF_SUPPORT_HEX
EndEnumeration

Import "shlwapi.lib"
  CompilerIf #PB_Compiler_Unicode
  StrToInt(string.s)                            As "_StrToIntW@4"
  StrToIntEx(string.s, dwFlags.l, *pLong)       As "_StrToIntExW@12"
  StrToInt64Ex(string.s, dwFlags.l, *pQuad)     As "_StrToInt64ExW@12"
  StrFormatByteSize64(num.q, *buffer, length.l) As "_StrFormatByteSizeW@16"
  CompilerElse
  StrToInt(string.s)                            As "_StrToIntA@4"
  StrToIntEx(string.s, flags.l, *pLong)         As "_StrToIntExA@12"
  StrToInt64Ex(string.s, flags.l, *pQuad)       As "_StrToInt64ExA@12"
  StrFormatByteSize64(num.q, *buffer, length.l) As "_StrFormatByteSize64A@16"
  CompilerEndIf
EndImport

ProcedureDLL.l StrToLong(string.s)
  
  Protected result.l = -1
  
  StrToIntEx(ReplaceString(string, "$", "0x"), #STIF_SUPPORT_HEX, @result) 
  
  ProcedureReturn result
  
EndProcedure
ProcedureDLL.q StrToQuad(string.s)
  
  Protected Result.q = -1
  
  StrToInt64Ex(ReplaceString(string, "$", "0x"), #STIF_SUPPORT_HEX, @Result) 
  
  ProcedureReturn Result
  
EndProcedure
ProcedureDLL.s StrFormatSize(num.q)
  
  Protected *string, buf.s = Space(255)
  
  *string = StrFormatByteSize64(num, @buf, 255)
  
  If *string
    ProcedureReturn PeekS(*string, 255)
  EndIf
  
EndProcedure

Debug StrToLong("$1000") 
Debug StrToQuad("$10000") 

Debug StrFormatSize(1000)
Debug StrFormatSize(10000)
Debug StrFormatSize(1000000)
Debug StrFormatSize(100000000)
Debug StrFormatSize(10000000000)

Debug StrFormatSize(StrToQuad("0x10000000"))
No programming language is perfect. There is not even a single best language.
There are only languages well suited or perhaps poorly suited for particular purposes. Herbert Mayer
Num3
PureBasic Expert
PureBasic Expert
Posts: 2812
Joined: Fri Apr 25, 2003 4:51 pm
Location: Portugal, Lisbon
Contact:

Post by Num3 »

Fluid Byte wrote:The heck.........

Google does math? Nice! 8)

Thanks for clearing up Trond.
I wonder what google CAN'T do !!!!! :roll: :roll:
Post Reply