AutoHotkey Community

It is currently May 26th, 2012, 4:03 pm

All times are UTC [ DST ]




Post new topic Reply to topic  [ 52 posts ]  Go to page 1, 2, 3, 4  Next
Author Message
PostPosted: April 11th, 2009, 9:34 pm 
Offline

Joined: November 4th, 2008, 9:23 am
Posts: 1045
Functions: autoByteFormat

Description
  • Converts from a specified size (in byes) to an equivalent value in byte(s)/KB/MB/GB/TB - uses the most appropriate unit.


Download
autoByteFormat.zip

Requirements
AHK v1.0.48+



Functions
autoByteFormat(size, decimalPlaces = 2)
Converts from a specified size (in byes) to an equivalent value in byte(s)/KB/MB/GB/TB - uses the most appropriate unit.

See the wiki page for file size for details on what each of these units are. This function uses the "Binary Measurement" (see the conversion table for details).

Parameters
size - integer size (in bytes)
decimalPlaces - decimal places to round result. If return is in bytes, no rounding is done, since it's an integer.

ReturnValue
Formatted size in bytes(s)/KB/MB/GB/TB

Format:

if size = 1 (byte)
returns "1 byte"

else if size < 1KB (1024 bytes)
returns "X bytes"

else if size < 1MB (1024 * 1024 bytes)
returns "X KB"

else if size < 1GB (1024 * 1024 * 1024 bytes)
returns "X MB"

else if size < 1TB (1024 * 1024 * 1024 * 1024 bytes)
returns "X GB"

else
returns "X TB"

"X" is the rounded value (to the specified decimal places), in the appropriate units.

Remarks
Size MUST be greater than or equal to zero.



Code
Code:
;converts Size (in bytes) to byte(s)/KB/MB/GB/TB (uses best option)
;decimalPlaces is the number of decimal places to round
autoByteFormat(size, decimalPlaces = 2)
{
    static size1 = "KB", size2 = "MB", size3 = "GB", size4 = "TB"

    sizeIndex := 0

    while (size >= 1024)
    {
        sizeIndex++
        size /= 1024.0

        if (sizeIndex = 4)
            break
    }

    return (sizeIndex = 0) ? size " byte" . (size != 1 ? "s" : "")
        : round(size, decimalPlaces) . " " . size%sizeIndex%
}


Example
Code:
1MB := 1024 * 1024
1@26MB := 1.26 * 1024 * 1024
1000@27GB := 1000.27 * 1024 * 1024 * 1024
17@28TB := 17.28 * 1024 * 1024 * 1024 * 1024

Bytes =
(LTrim
    0
    1
    2
    100
    1024
    1500
    %1MB%
    %1@26MB%
    %1000@27GB%
    %17@28TB%
)

Loop, Parse, Bytes, `n
{
    MsgBox, % A_LoopField " bytes = " autoByteFormat(A_LoopField)
}




How to use
Extract the zip's contents to a library folder for automatic inclusion - StdLib compliant.

A copy of the above example can be found in the "Func Examples" folder.


Download autoByteFormat function

_________________
As always, if you have any further questions, don't hesitate to ask.

Add OOP to your scripts via the Class Library. Check out my scripts.


Last edited by animeaime on April 12th, 2009, 5:15 pm, edited 9 times in total.

Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: April 12th, 2009, 3:11 am 
Offline

Joined: February 14th, 2005, 4:05 pm
Posts: 4710
Location: Boulder, CO
Thanks for posting this. You probably know it already that MB, GB and TB means 1e6, 1e9 and 1e12 bytes, respectively, if disk manufacturers use them to specify the capacity of their drives, so there will be always some confusions.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: April 12th, 2009, 3:17 am 
Offline

Joined: November 4th, 2008, 9:23 am
Posts: 1045
I used KB, MB, GB, and TB as they are commonly defined (like shown in the file's properties)

e.g.
1KB = 1024 bytes.

Hopefully, this doesn't cause too much confusion. I'll add a link to the wiki page for file size to shed light where otherwise might have been confusion. Thanks for the heads up.

_________________
As always, if you have any further questions, don't hesitate to ask.

Add OOP to your scripts via the Class Library. Check out my scripts.


Last edited by animeaime on April 13th, 2009, 6:17 am, edited 1 time in total.

Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: April 12th, 2009, 5:28 am 
Offline
User avatar

Joined: December 26th, 2005, 4:40 pm
Posts: 8776
There is an API function for this: StrFormatByteSize64, but I guess the function is limited to GB.
A working example can be found here.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: April 12th, 2009, 7:35 am 
You should stop requiring readers to download a zip file from your account on autohotkey.net (or anywhere else for that matter).

There are three reasons I say this.

First, I have noticed that several of your functions are only a couple of hundred bytes long. There is no reason to not post it in the forum, so readers can look at the code without having to download and unpack.

Second, by posting the code in the forum, readers can see when the topic/code was last edited (alerting the reader to an update/change).

Third, your account on autohotkey.net will not be maintained forever (as will posting in the forum). If your account becomes inactive for a long period of time, I believe that the account (and its contents will be deleted). I know that after not having accessed my account for about a year, I was sent a notice that if I did not login, my account would be deleted. So, if (and when) you move on, the linked downloads may not be available.

Finally (and this is just my personal reaction). After having downloaded several of your short functions, I am now not inclined to download and examine the zip files if the function (as in this case) seems to me to be 'trivial' and hardly worth the time to examine (maybe my loss). Whereas if the code is posted, I am more likely to examine it and then decide if it is worth keeping.

This topic of posting/downloading has come up in the forum before (although I cannot locate it now), and if I recall correctly, most readers that replied to the topic prefered that the code be posted.

Thanks for reading this.


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: April 12th, 2009, 11:47 am 
Offline
User avatar

Joined: August 11th, 2004, 1:47 am
Posts: 5347
Location: UK
Anonymous wrote:
your account on autohotkey.net will not be maintained forever (as will posting in the forum). If your account becomes inactive for a long period of time, I believe that the account (and its contents will be deleted). I know that after not having accessed my account for about a year, I was sent a notice that if I did not login, my account would be deleted. So, if (and when) you move on, the linked downloads may not be available.
I was notified about this so I will respond here. What you have said is untrue and misleading. Newly created accounts with no activity (logins, uploads or downloads) are flagged then removed after a month. The mass email sent a while ago requested users who had not used the new web manager to log in so their database profile could be automatically transferred. If you could read properly you would have known this. The account removal only applied under the said circumstance. Get your facts straight before posting next time.

_________________
GitHubScriptsIronAHK Contact by email not private message.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: April 12th, 2009, 5:07 pm 
Offline

Joined: November 4th, 2008, 9:23 am
Posts: 1045
Guest wrote:
There is no reason to not post it in the forum, so readers can look at the code without having to download and unpack.

Thank you for your input. I was trying to be "neat", but I see your point. I'll post the code on all my topics (both future and existing ones). Thank you for your feedback.


Guest wrote:
Second, by posting the code in the forum, readers can see when the topic/code was last edited (alerting the reader to an update/change).

If I update my code, I'll make a quick post. This allows anyone that watches the thread to be able to download the latest version.

Guest wrote:
Finally (and this is just my personal reaction). After having downloaded several of your short functions, I am now not inclined to download and examine the zip files if the function (as in this case) seems to me to be 'trivial' and hardly worth the time to examine (maybe my loss). Whereas if the code is posted, I am more likely to examine it and then decide if it is worth keeping.

I know exactly how you feel - truthfully, I felt ashamed posting these. They are short, and not anything "amazing". I posted them, because they are functions that I think should be "standardized". Additionally, I'm going to be posting some scripts that make use of some of these "nicknacks", so I wanted to post them, and get feedback, before posting the scripts. This way, I could get valuable feedback, and make necessary adjustments.

_________________
As always, if you have any further questions, don't hesitate to ask.

Add OOP to your scripts via the Class Library. Check out my scripts.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: April 12th, 2009, 5:09 pm 
Offline

Joined: November 4th, 2008, 9:23 am
Posts: 1045
SKAN wrote:
There is an API function for this: StrFormatByteSize64, but I guess the function is limited to GB.
A working example can be found here.

Thanks for brining up this function, I was not aware of it. Is there a way to control the number of decimal places it rounds to?

Edit:
What do you mean it only works for GB? The API function has other examples as well.

_________________
As always, if you have any further questions, don't hesitate to ask.

Add OOP to your scripts via the Class Library. Check out my scripts.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: April 13th, 2009, 3:50 am 
Offline
User avatar

Joined: December 26th, 2005, 4:40 pm
Posts: 8776
animeaime wrote:
What do you mean it only works for GB? The API function has other examples as well.


I had thought the function would not work for bytes exceeding GB. I was wrong!

Code:
Bytes := 9223372036854775807,   VarSetCapacity( Formatted,24,0 )
DllCall( "shlwapi.dll\StrFormatByteSize64A", Int64,Bytes, Str,Formatted, UInt,16 )
MsgBox, % Formatted


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: April 13th, 2009, 4:23 am 
Offline

Joined: February 14th, 2005, 4:05 pm
Posts: 4710
Location: Boulder, CO
StrFormatByteSize... seems to round the result to only 3 digits, which are sometimes too few.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: April 13th, 2009, 4:32 am 
Offline

Joined: November 4th, 2008, 9:23 am
Posts: 1045
Also, for some odd reason, an example they gave isn't the rounded result, and it only has one digit of precision.

Code:
Bytes := 23506,   VarSetCapacity( Formatted,24,0 )
DllCall( "shlwapi.dll\StrFormatByteSize64A", "Int64",Bytes, "Str",Formatted, "UInt",16 )

;outputs 22.9 KB
MsgBox, % Formatted

;outputs 22.96 KB
MsgBox, % autoByteFormat(bytes)

_________________
As always, if you have any further questions, don't hesitate to ask.

Add OOP to your scripts via the Class Library. Check out my scripts.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: April 13th, 2009, 4:34 am 
Offline

Joined: July 30th, 2007, 11:32 pm
Posts: 581
animeaime wrote:
I used KB, MB, GB, and TB as they are properly defined (like shown in the file's properties)


As Lazslo said, KB, MB, etc... are officially defined as SI units by which KB means 1000^1 bytes, MB means 1000^2 bytes, etc...
Some organizations have been pushing for the use of the term "kibibyte", "mibibyte", etc... to mean 1024^1, 1024^2, etc... but it's not exactly catching on.
Anyways, just wanted to let you know that, although a lot of programs (like Windows) use the binary definition, it officially isn't so.
Unfortunately, this confusion has caused many problems to consumers (as well as lawsuits against some companies, like Seagate).


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: April 13th, 2009, 4:53 am 
Offline

Joined: November 4th, 2008, 9:23 am
Posts: 1045
I know this may sound rude, and I appoligize before saying it. Since it can cause confusion, I'm not sure what to do then. Do you have a recommendation?

Would removing the phrase "properly defined" do the job :D? I realize this can be confusing, and I want to clear up that confusion.

The function uses the measurement I've seen used in every program I've used (including windows itself). I mean, the StrFormatByteSize function that SKAN pointed out uses the same format.

So, I'm at a loss. Truthfully, at this point, I'm confused about who is confused. The ONLY time I have ever heard the decimal usage of GB is by the makers of hard drives to save some of their money. I mean, every program, even Windows, uses the binary, but still the hard drive manufactures want to "claim" my computer with 160 024 174 592 bytes between it's four partions is a 160GB hard drive when every program says that all I have is autoByteFormat(160 024 174 592) = 149.03 GB.

_________________
As always, if you have any further questions, don't hesitate to ask.

Add OOP to your scripts via the Class Library. Check out my scripts.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: April 13th, 2009, 5:13 am 
Offline

Joined: April 18th, 2008, 7:57 am
Posts: 1390
Location: The Interwebs
animeaime, although that definition is the most commonly used, the Wikipedia article clearly states that for the powers of two definition, the proper prefix is Ki, Mi, Gi, etc. rather than K, M, G, etc. because those prefixes are used to denote powers of ten. However, the article is a bit contradictory, since the conversion table states the binary definition for the Kilo, Mega, Giga, etc prefixes...

In my opinion, the binary definition is definitely the one that should be used in this function, and you have clearly stated that you have used it, so I think you have done everything correctly.

Quote:
Would removing the phrase "properly defined" do the job Very Happy? I realize this can be confusing, and I want to clear up that confusion.


I think changing "properly defined" to "commonly defined" would fit better.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: April 13th, 2009, 6:15 am 
Offline

Joined: November 4th, 2008, 9:23 am
Posts: 1045
Thank you all for your input on this matter - the function will remain as it is.

I changed "properly defined" to "commonly defined" as suggested by Krogdor.

_________________
As always, if you have any further questions, don't hesitate to ask.

Add OOP to your scripts via the Class Library. Check out my scripts.


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 52 posts ]  Go to page 1, 2, 3, 4  Next

All times are UTC [ DST ]


Who is online

Users browsing this forum: Yahoo [Bot] and 14 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group