Solved

convert System::String to int or float

Posted on 2006-10-25
6
366 Views
Last Modified: 2008-02-01
Hi, I want to convert a string to a number.  I use code like this atm:

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

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?

Thanks,
0
Comment
Question by:SilentBob
  • 3
  • 3
6 Comments
 
LVL 48

Accepted Solution

by:
AlexFM earned 50 total points
ID: 17801681
For hexadecimal numbers use Int32.Parse Method (String, NumberStyles) with style parameter HexNumber.
0
 

Author Comment

by:SilentBob
ID: 17801688
how do I determine if my string is correctly formatted as a hex number?
0
 
LVL 48

Expert Comment

by:AlexFM
ID: 17801735
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.
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 

Author Comment

by:SilentBob
ID: 17810460
When I use:

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

or

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?
0
 
LVL 48

Expert Comment

by:AlexFM
ID: 17811070
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.
0
 

Author Comment

by:SilentBob
ID: 17811089
yeah, I worked that out eventually; I manually parse them now.

Thanks for the help
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now