Custom Number Format for hex

P

PIEBALD

I'm working on a utility that will allow the user to query some data from a
database and output it.
I would like to support allowing the user to provide a format for the
numbers, particularly if hexadecimal representation is desired.
Ideally I could simply do ((System.IFormattable) datarow [ columnname
]).ToString ( format , null ), but when hexadecimal is requested the user may
(should) want to tack on a leading "0x" and I've been unable to form a format
that will do that.

What should work is "'0x'X4", the '0x' should be copied to the output and
then the value should be formatted with X4. But, that doesn't seem to work,
even though the documentation seems to say it should.

The closest I've gotten is string.Format ( format , datarow [ columnname ] )
with a format of "0x{0:X4}", but the user shouldn't need to specify the
braces and such.

Am I mistaken that "'0x'X4" should work? Or is it a bug?
 
G

Göran Andersson

PIEBALD said:
I'm working on a utility that will allow the user to query some data from a
database and output it.
I would like to support allowing the user to provide a format for the
numbers, particularly if hexadecimal representation is desired.
Ideally I could simply do ((System.IFormattable) datarow [ columnname
]).ToString ( format , null ), but when hexadecimal is requested the user may
(should) want to tack on a leading "0x" and I've been unable to form a format
that will do that.

What should work is "'0x'X4", the '0x' should be copied to the output and
then the value should be formatted with X4. But, that doesn't seem to work,
even though the documentation seems to say it should.

The closest I've gotten is string.Format ( format , datarow [ columnname ] )
with a format of "0x{0:X4}", but the user shouldn't need to specify the
braces and such.

Am I mistaken that "'0x'X4" should work? Or is it a bug?

No, it's not a bug.

When formatting a number with the ToString method you use a numeric
format string. You can use a standard numeric format string, which
supports the x for hexadecimal, or you can use a custom numeric format
string that supports literals, but you can't combine them in a single
numeric format string.
 
B

Ben Voigt [C++ MVP]

PIEBALD said:
Ah, that makes sense. I don't like it; but it makes sense.

You can apparently add new format strings of your own choosing though, see
http://msdn.microsoft.com/en-us/library/0asazeez.aspx

So as long as you can imagine them before they are needed, you can save your
users from having to specify anything extra like braces.

Of course since you're the one calling the format function you can test for
your custom formats directly, no need for ICustomFormatter.

The trouble seems to be that the custom formats aren't powerful enough to
replicate all the cooked ("standard") format options.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top