AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

[Func] autoByteFormat - convert bytes to byte(s)/KB/MB/GB/TB
Goto page Previous  1, 2, 3, 4
 
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions
View previous topic :: View next topic  
Author Message
animeaime



Joined: 04 Nov 2008
Posts: 1046

PostPosted: Fri Apr 17, 2009 8:08 pm    Post subject: Reply with quote

For those that missed it, I'm still looking for feedback how to treat "00" when rounding. See my original post. Right now, it formats according to the present float format float format - see SetFormat for details.


Another problem with hex mode.

Because in hex mode, "a-f" are now "digits", this can invite some problems. From my testing, the problem ONLY occurs if these events "conspire"

As a note, the trailing "d" will treat the inputted size (if in bytes), as "decimal bytes" - e.g. 1 KB = 1000 bytes (the default behavior is to treat them as "binary bytes" - e.g. 1 KB = 1024 bytes). A trailing "b" would also cause the problem. The trailing "b" would treat the inputted value as a binary size (versus a decimal size). If the input is in bytes, it is interpretted as "binary bytes" by default.

1a) The size is stored in a variable (in hex format)

Code:
SetFormat, IntegerFast, hex

size := 5

;outputs 0x5
MsgBox, % size

;the trailing "d" is misinterpreted as part of the digit - "0x5d"
MsgBox, % autoByteFormat(size "d")


1b) The size is part of the parameter, in hex mode (why would do this) - "0x5d"

Code:
MsgBox, % autoByteFormat(0x5 "d")


Even if in hex mode, if the size is passed in decimal notation, there is no problem (since AHK doesn't convert the value to hex).
Code:
SetFormat, IntegerFast, hex

;in this case, the input is "5b", so it's not seen as hex - no "0x" prefix
MsgBox, % autoByteFormat(5 "b")



Surrounding the value in double quotes will prevent the problem - since AHK doesn't convert the value. Of course, if you enter the value is hex format - it's your own stupidity Very Happy

However, this only works if you don't later use the value in an math operation. If you stored the byte size (as an integer), and did math to it, then it would be converted after the operation - so, the problem will still exist.

Code:
SetFormat, IntegerFast, hex

size := "5"

;outputs "5"
MsgBox, % size

;doubled the size
size *= 2

;outputs "0xa"
MsgBox, % size

;the trailing "d" is misinterpreted as part of the digit - "0xad"
MsgBox, % autoByteFormat(size "d")



So, at this point there are two options.

1) Assume the value will never be in hex form. This, in my opinion might not be the best option.

For example, autoByteFormat can work directly with the return from a call to the FileGetSize command - provided the return is in bytes (which is the default).

If you are in hex mode, the return for the FileGetSize command is in hex format. Therefore, to allow this scenario, I would recommend the second option.

2) Specify a "separator" that MUST be used before the trailing "b", or "d" (if specified). I was thinking a ":", but I'm open to suggestions. If the trailing "i" is used, there will be no need for it (since "i" is not a valid hex digit), so it will be optional. It will still be allowed - for consistency.

Note: this separator will only be required if the size is in hex format. If in decimal, the separator will be optional - for consistency.


So, this call would look like this.

Code:
;example using FileGetSize
SetFormat, IntegerFast, hex

FileGetSize, FileSize, someFile.txt

;the trailing "d" wouldn't be misinterpreted as a hex digit
MsgBox, % autoByteFormat(FileSize ":d")


The other scenario that introduces problems is when using "EB" or "EiB" as the units - exabytes and exbibytes, respectively. However, this only occurs if "merging" a size and unit. If the size and unit are together (like most cases), and you specify the size in decimal units (which you should), then there will be no need for the ":", but it will be allowed - for consistency.

Code:
;example by "merging" size and unit
;possible scenarios include a GUI converter which has a drop down for sizes, units, etc - e.g. a GUI that makes use of this function.

SetFormat, IntegerFast, hex

size := 5
unit := "eb"

;separates the size and unit
separator := ""

;trailing is blank, so the value is interpreted based on unit
;(decimal for decimal units, and binary for binary units)
trailing := ""

;outputs 0x5eb (the "eb" is misinterpreted as part of the number)
MsgBox, % size . unit

;the "eb" wouldn't be misinterpreted as hex digits
MsgBox, % autoByteFormat(size . separator ":" unit . trailing, "D,")


Note, the above format is a "universal" format. It will work in all cases: if size is in hex or decimal, for any unit, and for any "trailing" (either "b", "i", "d", or none (blank)). The usage for the "trailing" will be described (in detail) on the webpage.


So, I'm thinking that this option is best. The downside is if a ":" was meant to be used as the separator between the size and unit, there would need to be two of them. If only one was used, it wouldn't be seen as the separator (when "parsing" the input for the settings, such as the separator). Note: the actual result (the size) would be the same regardless, but the default separator wouldn't be.

However, if you follow the above "template", then <separator> would equal ":", and there would be no problem.

Code:
;Some examples (and their output)

;outputs 5,000,000.00TB
MsgBox, % autoByteFormat("5:eb", "D,")

;outputs 5,000,000.00TB
MsgBox, % autoByteFormat("5eb", "D,")

;outputs 5,000,000.00:TB
MsgBox, % autoByteFormat("5::eb", "D,")

;outputs 5,000,000.00 TB
MsgBox, % autoByteFormat("5 :eb", "D,")

;outputs 5,000,000.00: TB
MsgBox, % autoByteFormat("5: eb", "D,")

;outputs 5,000,000.00: TB
MsgBox, % autoByteFormat("5: :eb", "D,")

_________________
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.
Back to top
View user's profile Send private message Send e-mail
Drugwash



Joined: 08 Sep 2008
Posts: 608
Location: Ploiesti, RO

PostPosted: Fri Apr 17, 2009 8:29 pm    Post subject: Reply with quote

Had a headache yesterday and managed to get rid of it. Now I got another one, after reading your post. Very Happy You owe me a painkiller. Razz

In short: would $ qualify for a binary/decimal type separator, instead of the colon ( : ) ?
Back to top
View user's profile Send private message Yahoo Messenger
animeaime



Joined: 04 Nov 2008
Posts: 1046

PostPosted: Fri Apr 17, 2009 8:34 pm    Post subject: Reply with quote

Drugwash wrote:
Had a headache yesterday and managed to get rid of it. Now I got another one, after reading your post. Very Happy You owe me a painkiller. Razz

In short: would $ qualify for a binary/decimal type separator, instead of the colon ( : ) ?

You have the headache...come on - I found the scenarios. This function requires a lot of testing... Very Happy


So use "$" instead of a ":"?

ex.
Code:
;outputs 5,000,000.00 TB
MsgBox, % autoByteFormat("5 $eb", "D,")

_________________
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.
Back to top
View user's profile Send private message Send e-mail
Drugwash



Joined: 08 Sep 2008
Posts: 608
Location: Ploiesti, RO

PostPosted: Fri Apr 17, 2009 8:51 pm    Post subject: Reply with quote

Yes, that's the idea. Should work fine and be visible enough to draw attention. Of course, other opinions are welcome.

Oh and you're right... testing is a heavy task, you may use that painkiller. Razz
Back to top
View user's profile Send private message Yahoo Messenger
animeaime



Joined: 04 Nov 2008
Posts: 1046

PostPosted: Fri Apr 17, 2009 8:55 pm    Post subject: Reply with quote

Drugwash wrote:
Yes, that's the idea. Should work fine and be visible enough to draw attention. Of course, other opinions are welcome.

OK, I'll keep that as an idea. I still have some more testing to do, and finish adding in the latest features to the web page, so there's still time for others to chime in.

Drugwash wrote:
Oh and you're right... testing is a heavy task, you may use that painkiller. Razz

Have any to spare. Very Happy
_________________
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.
Back to top
View user's profile Send private message Send e-mail
Drugwash



Joined: 08 Sep 2008
Posts: 608
Location: Ploiesti, RO

PostPosted: Fri Apr 17, 2009 8:59 pm    Post subject: Reply with quote

Sure, lemme grab the slingshot and I'll send it to you. Very Happy
Back to top
View user's profile Send private message Yahoo Messenger
animeaime



Joined: 04 Nov 2008
Posts: 1046

PostPosted: Fri Apr 24, 2009 12:52 pm    Post subject: Reply with quote

Sorry for the lack of progress - been having some personal problems (depression). However, I have been working bit by bit on the documentation for the function. The function is done - except for the tweaking that will be done after the beta release, but the docs are taking forever - especially when I can't concentrate or focus. However, just wanted to say that I haven't given this up - it's just taking longer than I would like.

I do come today with a question. The function supports scientific notation, for input. The default behavior is to have decimal notation for output, but there is an option to force scientific notation for output. I'm wondering, when should scientific notation be the default for output? Maybe this is too rare of a case to worry about at this time - just wanted some feedback.

Some venting on my part: I'm way over my head with all these projects. It's really annoying when programming takes a day or two, but docs take a week plus. Sad OK, I feel better with that off my chest.
_________________
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.
Back to top
View user's profile Send private message Send e-mail
Display posts from previous:   
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions All times are GMT
Goto page Previous  1, 2, 3, 4
Page 4 of 4

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group