Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

convert System::String to int or float

Posted on 2006-10-25
6
Medium Priority
?
384 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 200 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
Technology Partners: 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

Technology Partners: 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

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
Article by: evilrix
Looking for a way to avoid searching through large data sets for data that doesn't exist? A Bloom Filter might be what you need. This data structure is a probabilistic filter that allows you to avoid unnecessary searches when you know the data defin…
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 clear a vector as well as how to detect empty vectors in C++.

618 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