Page 1 of 1
Correct filesize calculation
Posted: Wed Oct 25, 2006 6:48 pm
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 ?
Posted: Wed Oct 25, 2006 7:58 pm
by Trond
Posted: Wed Oct 25, 2006 8:18 pm
by Fluid Byte
The heck.........
Google does math? Nice!
Thanks for clearing up Trond.
Posted: Thu Oct 26, 2006 8:35 pm
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
Posted: Thu Oct 26, 2006 8:50 pm
by Trond
Without looking at the code logic I'd recommend you to use StrD() instead of StrF().
Posted: Thu Oct 26, 2006 9:10 pm
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

Posted: Thu Oct 26, 2006 9:58 pm
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"))
Posted: Fri Oct 27, 2006 11:58 am
by Num3
Fluid Byte wrote:The heck.........
Google does math? Nice!
Thanks for clearing up Trond.
I wonder what google CAN'T do !!!!! :roll: :roll: