Page 1 of 1

Format("{1:#08x}", 0) ; 00000000 instead of 0x000000

Posted: 11 Aug 2019, 09:21
by Thineboot

Code: Select all

h := Format("{:#08X} {:#08x} {:#06x} {:#08x} {:#06x} 0x{:06x} ", 0, 1, 1, 0xffffff, 0xffffff, 0)
i := Format("{:04} {:04} {:04} ", 4, +4, -4)
MsgBox %h%`n%i%
;	00000000 0x000001 0x0001 0xffffff 0xffffff 0x000000
;	0004 0004 00-4
Format("{1:#08x}", 0) doesn't insert x or X but 0.
For any positive number other than zero it works as expected.
When dealing with colors it's a bit annoying to work around emulating a deprecated style.

The doc reads "Unsigned hexadecimal integer; uses "abcdef" or "ABCDEF" depending on the case of x. The 0x prefix is not included unless the # flag is used, as in {:#x}. For hexadecimal formatting consistent with SetFormat, use 0x{:x} or similar." Well, you have to add 2 to the width for the prefix but it makes sense as width is width.
Just out of curiosity I've checked negative integers and it doesn't handle them well either.

Re: Format("{1:#08x}", 0) ; 00000000 instead of 0x000000

Posted: 11 Aug 2019, 12:19
by swagfag
everything is according to spec. regarding ur first problem, it says:
# wrote:When it's used with the o, x, or X format, the # flag uses 0, 0x, or 0X, respectively, to prefix any nonzero output value.
if u pass 0 in, the flag doesnt kick in, and ur 0 only gets leftpadded out to 8 places(with zeros).

regarding ur second problem, it says:
Type wrote:a character indicating how the input value should be interpreted. If omitted, it defaults to s.
so if u wanna pass -4 in and get it leftpadded out to 4 places, including the - sign and also have it remain in front, u have to do this:

Code: Select all

Format("{:+04i}", -4)

Re: Format("{1:#08x}", 0) ; 00000000 instead of 0x000000

Posted: 11 Aug 2019, 12:52
by Thineboot
As for the original case you're right, missed your "#" quote. Does it make sense to you to exclude zero? Is there a reason I'm not aware of why this is necessary? Cause it's "0", not "" and each color channel ranges from 0x00 to 0xFF meaning zero is not that rare.

Thanks for clarifying the second part. As I've said, it was just out of curiosity and I've obviously not checked the "+" flag. So technically it's ok to show 00-4 it just doesn't feel ok if you get my point.

Re: Format("{1:#08x}", 0) ; 00000000 instead of 0x000000

Posted: 11 Aug 2019, 13:26
by swagfag
i get ur point but i dont know what to tell u. format simply forwards to printf and this is how printf works. can it be changed to accommodate an edge case? probably. will it? in v1 certainly not, in v2 perhaps but likely not.

Re: Format("{1:#08x}", 0) ; 00000000 instead of 0x000000

Posted: 11 Aug 2019, 15:08
by Thineboot
I'm pretty sure there is lib out there for "... Visual C++ 2010 toolset..." that provides an alternative for printf but it's not up to me to decide.
Have I lately made the impression of not liking printf? Just unintentional ;)