Suggestion: Binary Integers
Suggestion: Binary Integers
I was kinda surprised when I realized there was no support for binary integers—not even in format. I think a 0b prefix, or at least a b format type, would be great.
Re: Suggestion: Binary Integers
- I have said that 0b... would be nice to match 0x... which we already have.
- Is it especially common? I know that Java and Python support it, but not C++.
- I would definitely like something like BaseToDec and DecToBase (in some form). Out of interest, do you actually use 0b... that often? Thanks.
- Is it especially common? I know that Java and Python support it, but not C++.
- I would definitely like something like BaseToDec and DecToBase (in some form). Out of interest, do you actually use 0b... that often? Thanks.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
Re: Suggestion: Binary Integers
I do not use it often, but recently something got me looking for it, only to realize it wasn't there (I don't remember what that thing was). But octal is a format type, is octal used more than binary?
Re: Suggestion: Binary Integers
- The Format function accepts octal, good point. Once you add a letter to Format, there's no turning back. So even though I think adding b for binary might be a good idea, I'm cautious. (Dec to/from Base functions, like I mentioned, would definitely be welcome.)
- I use bin, dec, hex regularly, but never oct, if anyone would like to say why octal is useful. Those are the classic 4 that usually have special functions/syntax.
- One time when 0b would have been useful was while I was writing my CRC-32 tutorial.
[EDIT:] 'b' does look like a good candidate for Format.
- I use bin, dec, hex regularly, but never oct, if anyone would like to say why octal is useful. Those are the classic 4 that usually have special functions/syntax.
- One time when 0b would have been useful was while I was writing my CRC-32 tutorial.
[EDIT:] 'b' does look like a good candidate for Format.
Last edited by jeeswg on 13 May 2019, 04:04, edited 1 time in total.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
Re: Suggestion: Binary Integers
Most of the formatting for each value is done with a C runtime function similar to printf. All of the type characters and flags supported by Format except for U, L and T are implemented by passing them to this function. Octal is supported merely because the Microsoft's printf supports it; on our end it's just a case of adding one letter to the string of acceptable type characters. I'm not aware of any C runtime functions that support binary literals (parsing or formatting), and I am not inclined to write code for it myself since I won't find it useful.
Re: Suggestion: Binary Integers
As literals such as 0b0001 and 0o0007 are effectively reserved for future use by virtue of the fact that variable names cannot start with a digit in v2, I believe this could be implemented post-v2.0 without breaking scripts if someone is ever inclined to do so. I have moved the topic to Wish List.
Re: Suggestion: Binary Integers
Last edited by Helgef on 14 May 2019, 03:02, edited 1 time in total.
Re: Suggestion: Binary Integers
I've known that for a long time, and was reminded when I wrote tcstoi64_o(), which was months before that post, so I'm not sure why I posted that. Perhaps I was referring to the "0o" and "0b" prefixes, which we would need to recognize before calling those functions (or parsing it ourselves).
It's still simpler if we can just pass the formatting codes and values to sprintf or equivalent. On the other hand, that it does that is why Format's hex mode never prints negative values.
It's still simpler if we can just pass the formatting codes and values to sprintf or equivalent. On the other hand, that it does that is why Format's hex mode never prints negative values.
Who is online
Users browsing this forum: No registered users and 14 guests