Solved

convert System::String to int or float

Posted on 2006-10-25
6
371 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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

837 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