Bug with conversion bytes to GB

Find quick help here to get you started with Hollywood
Post Reply
User avatar
Juan Carlos
Posts: 933
Joined: Mon Sep 06, 2010 1:02 pm

Bug with conversion bytes to GB

Post 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.
User avatar
Juan Carlos
Posts: 933
Joined: Mon Sep 06, 2010 1:02 pm

Re: Bug with conversion bytes to GB

Post 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
djrikki
Posts: 682
Joined: Wed Apr 06, 2011 12:26 am

Re: Bug with conversion bytes to GB

Post 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
Evolve - Rapid GUI Development tool for MUI Royale and RapaGUI
http://myevolve.wordpress.com
User avatar
airsoftsoftwair
Posts: 5848
Joined: Fri Feb 12, 2010 2:33 pm
Location: Germany
Contact:

Re: Bug with conversion bytes to GB

Post 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.
User avatar
Juan Carlos
Posts: 933
Joined: Mon Sep 06, 2010 1:02 pm

Re: Bug with conversion bytes to GB

Post 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
User avatar
Juan Carlos
Posts: 933
Joined: Mon Sep 06, 2010 1:02 pm

Re: Bug with conversion bytes to GB

Post 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.
User avatar
Juan Carlos
Posts: 933
Joined: Mon Sep 06, 2010 1:02 pm

Re: Bug with conversion bytes to GB

Post 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
Post Reply