More on ConvertTo-FixedByte vs Powershell Community Extensions
Last time we introduced two undocumented cmdlets that shipped with the TFS Fall ‘08 power tools. The idea behind ConvertTo-FixedByte is probably familiar to anyone who’s used Format-Byte from PSCX. Take a number and make it pretty. That’s it -- even the dirt simple conversion from “0” to “<DIR>” is handled in the formatter XML, not the cmdlet.
However, there are some key differences:
- ConvertTo-FixedByte lets you specify any width between 6 and 22, while Format-Byte is fixed at 10 characters.
- ConvertTo-FixedByte allows the entire UInt64 range (~10^20), while Format-Byte only goes up to TB (~10^15).
- ConvertTo-FixedByte does not allow pipeline input, while Format-Byte does.
- ConvertTo-FixedByte uses the SI standard prefixes, as recommended by IEC, IEEE, EU, and NIST. Format-Byte uses binary-based prefixes (although it does not display them as KiB, MiB, etc).
PS> convertto-fixedbyte 2000 -width 10
2 KB
PS> format-byte 2000
1.953 KB - ConvertTo-FixedByte uses the biggest possible unit so long as the value is at least one unit. Format-Byte waits until you’ve exceeded 2^n to use the next biggest unit.
PS> convertto-fixedbyte 1000 -width 10
1 KB
PS> format-byte 1024
1024 B - Both cmdlets attempt to output a fixed width by rounding, but Format-Byte’s approach fails to handle some edge cases correctly:
PS> convertto-fixedbyte (1000*999) -width 10
999 KB
PS> convertto-fixedbyte (1000*999+1) -width 10
999.001 KB
PS> convertto-fixedbyte (1000*999+499) -width 10
999.499 KB
PS> convertto-fixedbyte (1000*999+500) -width 10
1 MB
PS> convertto-fixedbyte (1000*999+501) -width 10
1 MB
PS> convertto-fixedbyte (1000*1000-1) -width 10
1 MB
PS> convertto-fixedbyte (1000*1000) -width 10
1 MB
PS> convertto-fixedbyte (1000*1000+1) -width 10
1 MB
PS> format-byte (1024*1023)
1023 KB
PS> format-byte (1024*1023+1)
1023.001 KB
PS> format-byte (1024*1023+511)
1023.499 KB
PS> format-byte (1024*1023+512)
1023.5 KB
PS> format-byte (1024*1023+513)
1023.501 KB
PS> format-byte (1024*1024-1)
1023.999 KB
PS> format-byte (1024*1024)
1024 KB
PS> format-byte (1024*1024+1)
1 MB - Both cmdlets omit trailing 0’s, instead padding the front with extra spaces. [thought there was a slight difference here, but now I can’t repro it!]
- And of course: PSCX is open-source, while the Power Tools are not. So I can’t read the PSCX source and Keith can’t read mine. Even so, I hope some of the ideas can cross-pollinate :)