"Accelerator Types"

Topics: Developer Forum
Developer
Mar 24, 2008 at 9:37 PM
Hi guys, I got this idea few days ago. I often use powershell to get hexadecimal representation of a decimal number, and typing 42 -f '{0:x}' feels somewhat unfriendly. I thought we could make a Hex class, which would accept int64 in ctor and return the formatted string fromToString().
Likewise, we could add other common bases like Binary and Octal.

What do you think? Is it worth the risk that somebody comes up with the same class? How about putting it into a separate assembly?
Coordinator
Mar 26, 2008 at 3:40 PM
I like the idea. Something like a ConvertTo-Hex or ConvertTo-Base? I'd just put it in Pscx.dll assembly.
Developer
Mar 26, 2008 at 6:16 PM
Edited Mar 26, 2008 at 6:24 PM
jachymko, sometimes I think we're connected by some magic brain ethernet. I don't blog that often, but a couple of days ago, I started writing an entry about emmm, accelerator types. Seriously. I wrote a hex accelerator that works like:

# convert int (base10) to hex (base16). 
ps>[hex]16
10

you can also use standard arithmetic operators on it:

# lvalue is hex, so result is hex.
ps> [hex]16 * 10
A0

# convert string "FF" to decimal
ps> [int][hex]"FF"
255

It's not quite the same thing as using constructors, but instead I use implicit operator overloads, and the Hex type has no namespace ;-)

Look for my post later ( http://www.nivot.org )

- Oisin
Coordinator
Mar 27, 2008 at 12:03 AM
Doh! I guess I totally glossed over the "accelerator types" bit. Yeah that would be pretty convenient. Shouldn't the rendered output for hex look like 0xA0? So you can distinguish between 11 decimal and 11 hex (17 decimal).
Developer
Mar 27, 2008 at 11:41 AM
Oisin, how do you convert it back to string? Using a string operator or how? It should definitely output the 0x prefix.

Cool idea with the arithmetic. What would [hex]0x10 * 10 produce? BTW [int][hex]FF is nice, but simple 0xFF is even better ;-)
Developer
Mar 28, 2008 at 3:24 AM
Edited Apr 2, 2008 at 1:43 PM
blog posted at http://www.nivot.org/2008/03/27/CreateYourOwnCustomQuotTypeAcceleratorsquotInPowerShell.aspx

I updated it to use 0x prefix. Good idea.
Developer
Mar 28, 2008 at 12:23 PM
Edited Mar 28, 2008 at 12:24 PM


jachymko wrote:
Oisin, how do you convert it back to string? Using a string operator or how? It should definitely output the 0x prefix.

Cool idea with the arithmetic. What would [hex]0x10 * 10 produce? BTW [int][hex]FF is nice, but simple 0xFF is even better ;-)


PS C:\> 0xff * 2
510
PS C:\> [hex]"ff" * 2
 
0x1FE
 
PS C:\> [hex]0xff * 2
 
0x1FE
so with the [hex] operator, it stays as hex.