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 ?
Correct filesize calculation
- Fluid Byte
- Addict

- Posts: 2336
- Joined: Fri Jul 21, 2006 4:41 am
- Location: Berlin, Germany
Correct filesize calculation
Windows 10 Pro, 64-Bit / Whose Hoff is it anyway?
- Fluid Byte
- Addict

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

- Posts: 2336
- Joined: Fri Jul 21, 2006 4:41 am
- Location: Berlin, Germany
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$
EndIfWindows 10 Pro, 64-Bit / Whose Hoff is it anyway?
The intervals are correct but calculations are wrong.. you should divide by 1024^x to get the right valuesFluid 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
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
There are only languages well suited or perhaps poorly suited for particular purposes. Herbert Mayer


