convert System::String to int or float

Hi, I want to convert a string to a number.  I use code like this atm:

try {
      uint32_t d = System::Convert::ToUInt32(s);
} catch(...) {
      appendDebug(S"Input not a number");

But I want to be able to handle numbers written as 0x12F3 for instance.  Less important is being able to handle scientific notation.

Is there any pre-made functions to do this?

Who is Participating?
AlexFMConnect With a Mentor Commented:
For hexadecimal numbers use Int32.Parse Method (String, NumberStyles) with style parameter HexNumber.
SilentBobAuthor Commented:
how do I determine if my string is correctly formatted as a hex number?
You cannot handle hexadecimal and decimal numbers in the same function. For example, string "123" can be both hexadecimal or decimal.
Int32.Parse Method throws exception if string doesn't contain valid number. For example, if you use HexNumber parameter and string is not formatted correctly, FormatException is thrown.
The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

SilentBobAuthor Commented:
When I use:

uint32_t d = System::Int32::Parse(s, System::Globalization::NumberStyles::AllowHexSpecifier);


uint32_t d = System::Int32::Parse(s, System::Globalization::NumberStyles::HexNumber);

I can convert strings like "1FF" fine, but I get an exception when they are entered as "0x1FF"

Must I manually parse the string?
This looks strange, but AllowHexSpecifier style doesn' allow 0x:
Strings parsed using this style are not permitted to be prefixed with "0x" (MSDN).

You need to remove 0x manually before parsing.
SilentBobAuthor Commented:
yeah, I worked that out eventually; I manually parse them now.

Thanks for the help
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.