Solved

convert System::String to int or float

Posted on 2006-10-25
6
376 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

690 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