Page 1 of 1
Bug with conversion bytes to GB
Posted: Sun Mar 03, 2013 8:19 pm
by Juan Carlos
I found a bug when I tryed to convert the bytes in GB from one video file, this video has a size of 2.66.1294KB although Hollywoood gives this size in bytes, when I use the divide parameter Size1=Size\1073741824 the valour of Size1 is -2 and the float division too -2, "File size: -2,-2 GB" when I divide to get MB or KB the \ give me the correct result.
Re: Bug with conversion bytes to GB
Posted: Wed Mar 06, 2013 1:00 pm
by Juan Carlos
The code to convert:
Code: Select all
Tamano=FileSize(file$)
If Tamano=>1073741824
Tamano4=Tamano\1073741824
Tamano5=Tamano4%1073741824 ;Con el % nos devuelve los decimales.
Tamano6=LeftStr(Tamano5, 2) ;Nos devuelve 2 caracteres, en este caso dos decimales.
Size$=Tamano4..","..Tamano6.." GB"
ElseIf Tamano=>1048576
Tamano1=Tamano\1048576
Tamano2=Tamano%1048576 ;Con el % nos devuelve los decimales.
Tamano3=LeftStr(Tamano2, 2) ;Nos devuelve 2 caracteres, en este caso dos decimales.
Size$=Tamano1..","..Tamano3.." MB"
Else
Tamano=Tamano\1024
Size$=Tamano.." KB"
EndIf
Re: Bug with conversion bytes to GB
Posted: Wed Mar 06, 2013 4:58 pm
by djrikki
I have no idea about this bug, but here is a function that nevertheless you may find useful:
Local filesize, bytename = base:ConvertBytes(FileSize("T:filename"))
debugprint(filesize .. " " .. bytename)
Code: Select all
; Converts filesize in bytes to written notation eg; 1.1 TB, 10.5 GB, 11 MB, 309 KB, 90 bytes
Function base:ConvertBytes(filesize)
Local bytename
If filesize >= 1099511627776 ; greater than a tetrabyte
filesize = filesize / 1099511627776
filesize = MidStr(ToString(filesize),0,FindStr(ToString(filesize),".")+3)
bytename = "TB"
ElseIf filesize >= 1073741824 ; greater than a gigabyte
filesize = filesize / 1073741824
filesize = MidStr(ToString(filesize),0,FindStr(ToString(filesize),".")+3)
bytename = "GB"
ElseIf filesize >= 1048576 ; greater than a megabyte
filesize = filesize / 1048576
filesize = MidStr(ToString(filesize),0,FindStr(ToString(filesize),".")+3)
bytename = "MB"
ElseIf filesize >= 1024 ; greater than a kilobyte
filesize = filesize / 1024
filesize = MidStr(ToString(filesize),0,FindStr(ToString(filesize),".")+3)
bytename = "KB"
Else
bytename = "bytes"
EndIf
return(filesize, bytename)
EndFunction
Re: Bug with conversion bytes to GB
Posted: Wed Mar 06, 2013 9:52 pm
by airsoftsoftwair
Juan Carlos's problem might be related to the fact that the integer division operator (\) is limited to signed 32bit values. Thus, you'll run into a limit when dealing with files larger than ~2GB. The real division operator (/) uses 64bit values so this is the way to go if you have to deal with large numbers.
Re: Bug with conversion bytes to GB
Posted: Wed Mar 06, 2013 11:09 pm
by Juan Carlos
djrikki wrote:I have no idea about this bug, but here is a function that nevertheless you may find useful:
Local filesize, bytename = base:ConvertBytes(FileSize("T:filename"))
debugprint(filesize .. " " .. bytename)
Code: Select all
; Converts filesize in bytes to written notation eg; 1.1 TB, 10.5 GB, 11 MB, 309 KB, 90 bytes
Function base:ConvertBytes(filesize)
Local bytename
If filesize >= 1099511627776 ; greater than a tetrabyte
filesize = filesize / 1099511627776
filesize = MidStr(ToString(filesize),0,FindStr(ToString(filesize),".")+3)
bytename = "TB"
ElseIf filesize >= 1073741824 ; greater than a gigabyte
filesize = filesize / 1073741824
filesize = MidStr(ToString(filesize),0,FindStr(ToString(filesize),".")+3)
bytename = "GB"
ElseIf filesize >= 1048576 ; greater than a megabyte
filesize = filesize / 1048576
filesize = MidStr(ToString(filesize),0,FindStr(ToString(filesize),".")+3)
bytename = "MB"
ElseIf filesize >= 1024 ; greater than a kilobyte
filesize = filesize / 1024
filesize = MidStr(ToString(filesize),0,FindStr(ToString(filesize),".")+3)
bytename = "KB"
Else
bytename = "bytes"
EndIf
return(filesize, bytename)
EndFunction
Thanks I'll test it
Re: Bug with conversion bytes to GB
Posted: Wed Mar 06, 2013 11:12 pm
by Juan Carlos
Andreas wrote:Juan Carlos's problem might be related to the fact that the integer division operator (\) is limited to signed 32bit values. Thus, you'll run into a limit when dealing with files larger than ~2GB. The real division operator (/) uses 64bit values so this is the way to go if you have to deal with large numbers.
Yes, I though in the problem with long variables, as in this case.
Re: Bug with conversion bytes to GB
Posted: Thu Mar 07, 2013 12:30 pm
by Juan Carlos
I found a solution instead of make the divison with \ to sue the Div and there isn't the problem of overflow with the 64 bits dates, and the result is this routine between a mixed from djrikki and the mine, thanks djrikki because with the Size1$=MidStr(ToString(Tamano),0,FindStr(ToString(Tamano),".")+3) is very fast make the conversion
Code: Select all
Tamano=FileSize(file$)
If Tamano=>1073741824
Tamano=Div(Tamano, 1073741824)
Size1$=MidStr(ToString(Tamano),0,FindStr(ToString(Tamano),".")+3)
Size$=Size1$.." GB"
ElseIf Tamano=>1048576
Tamano=Div(Tamano, 1048576)
Size1$=MidStr(ToString(Tamano),0,FindStr(ToString(Tamano),".")+3)
Size$=Size1$.." MB"
Else
Tamano=Div(Tamano, 1024)
Size1$=MidStr(ToString(Tamano),0,FindStr(ToString(Tamano),".")+3)
Size$=Size1$.." KB"
EndIf