A
Andrew Lighten
Hi all;
I'm really struggling to get C# to do the right thing when parsing
non-decimal strings as numbers. What I really want to be able to do is
take a string containing an integer in any valid format (decimal, octal
with leading zero, hex with leading "0x") and have C# get the conversion
right.
I hope I'm missing something, but it seems like you have to know in
advance what format a string is in for it to be parsed correctly. None
of the valid NumberStyles flags seem to say "ok, look at the prefix to
work out what base it's in, then just do the damn conversion for me".
One of the NumberStyles enumeration flags is "AllowHexSpecifier". Great,
I thought -- this says that it will allow a leading "0x" and it will
recognise that it's a hex string. Nope. The docs go on to say, and I
quote MSDN, "Strings parsed using this style are not permitted to be
prefixed with "0X"". Yeah, that's real helpful. The AllowHexSpecifier
flag specifically disallows a hex specifier. Neato.
Can anyone point me in the right direction here? I just want a simple
strtol() equivalent that works.
Thanks,
Andrew.
I'm really struggling to get C# to do the right thing when parsing
non-decimal strings as numbers. What I really want to be able to do is
take a string containing an integer in any valid format (decimal, octal
with leading zero, hex with leading "0x") and have C# get the conversion
right.
I hope I'm missing something, but it seems like you have to know in
advance what format a string is in for it to be parsed correctly. None
of the valid NumberStyles flags seem to say "ok, look at the prefix to
work out what base it's in, then just do the damn conversion for me".
One of the NumberStyles enumeration flags is "AllowHexSpecifier". Great,
I thought -- this says that it will allow a leading "0x" and it will
recognise that it's a hex string. Nope. The docs go on to say, and I
quote MSDN, "Strings parsed using this style are not permitted to be
prefixed with "0X"". Yeah, that's real helpful. The AllowHexSpecifier
flag specifically disallows a hex specifier. Neato.
Can anyone point me in the right direction here? I just want a simple
strtol() equivalent that works.
Thanks,
Andrew.