Parsing non-decimal strings as numbers

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.
 
A

Andrew Lighten

SK said:
Try the following functions:

Convert.ToInt32(str);
System.Int32.Parse(str);

They're of no help at all, because they bork when presented with a hex
string.
 
A

Austin Ehlers

They're of no help at all, because they bork when presented with a hex
string.

Convert.ToInt32(str, 16) *will* work with the leading 0x.

In general, you can't convert without knowing something beforehand.
Take the string "123". What is it's numerical value?
In Base 10
Base 8 83
Base 10 123
Base 16 291

If you are sure that your string, if hex, comes with a leading "0x",
try:
if (str[1]=='x')
return Convert.ToInt32(str,16);
else return Convert.ToInt32(str,10);
 
A

Andrew Lighten

Austin said:
Convert.ToInt32(str, 16) *will* work with the leading 0x.

In general, you can't convert without knowing something beforehand.
Take the string "123". What is it's numerical value?
In Base 10
Base 8 83
Base 10 123
Base 16 291

If you are sure that your string, if hex, comes with a leading "0x",
try:
if (str[1]=='x')
return Convert.ToInt32(str,16);
else return Convert.ToInt32(str,10);

"123" has a numeric value of 123 decimal. It's a base 10 number.

If it was base 8 (octal) it would be "0123". If it was base 16 (hex) it
would be "0x123".

The whole point is that I'm not sure whether my string is a hex string
with a leading "0x", an octal string with a leading "0" or a decimal string.

What I want is the functionality that strtol() gives when you pass a
base of zero: it looks at the prefix to work it out for itself.
 
A

Austin Ehlers

<sniP
What I want is the functionality that strtol() gives when you pass a
base of zero: it looks at the prefix to work it out for itself.

You should have said that sooner. ;)

[DllImport("msvcrt.dll")] //MS Visual C Runtime
private static extern int strtol(string str,out string error,int
Base); //return value is int because C# int is the same as a C long

'Lib
 

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