Solved

Convert string to integer

Posted on 2014-04-07
10
211 Views
Last Modified: 2015-05-18
Is there any C++ function which will convert wide char string to int. If wide char string is double "100.01" then function should return error. I can use isDigit and add own logic to validate this but wanted to check whether there is any in build function for the same. I am using VC++ on windows.
0
Comment
Question by:RahulKajave
10 Comments
 
LVL 68

Assisted Solution

by:Qlemo
Qlemo earned 250 total points
Comment Utility
atoi / atof should have a wide string sibling
0
 
LVL 86

Accepted Solution

by:
jkr earned 250 total points
Comment Utility
'strtol()' is the 'standard', next to 'atoi()' that qlemo already mentioned, with the 1st allowing better error checking. See http://en.cppreference.com/w/cpp/string/byte/strtol

E.g.

#include <iostream>
#include <cstdlib>
using namespace std;

char* str = "100.01";
char* cnv_end;

long l = strtol(str,&cnv_end,10);

if (cnv_end != NULL) {

  cout << "Conversion failed, invalid character at " << cnv_end << end;

} else {

  cout << "Conversion succeded, value is " << l << endl;
}

Open in new window


Alternatively, you can use a 'std::stringstream' for that like

#include <iostream>
#include <string>
#include <sstream>
using namespace std;

char* str = "100.01";
int n;

stringstream ss;

ss << str;

ss >> n; // try to extract an integer

if (ss.fail()) {

  cout << "Conversion failed" << end;

} else {

  cout << "Conversion succeded, value is " << n << endl;
}

Open in new window

0
 

Author Comment

by:RahulKajave
Comment Utility
What about localization? Will strtol and stringstream handle localization properly. What if decimal separtor is "," instead of ".".
0
Complete Microsoft Windows PC® & Mac Backup

Backup and recovery solutions to protect all your PCs & Mac– on-premises or in remote locations. Acronis backs up entire PC or Mac with patented reliable disk imaging technology and you will be able to restore workstations to a new, dissimilar hardware in minutes.

 
LVL 86

Expert Comment

by:jkr
Comment Utility
Yup. In both cases the constraint is 'non-numeric character', regardless of the localization.
0
 
LVL 86

Expert Comment

by:jkr
Comment Utility
Do you still have issues with this or have you just not thought of closing the question?
0
 
LVL 8

Expert Comment

by:Subrat (C++ windows/Linux)
Comment Utility
// What about localization?
You can write a function to replace the special characters(, with .) and then pass through stringstream to get the double/int value if needed as specified above by JKR.
0
 
LVL 86

Expert Comment

by:jkr
Comment Utility
Um, integers won't have a double-type separator such as a comma or a semicolon in the 1st place ;o)
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

This collection of functions covers all the normal rounding methods of just about any numeric value.
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

744 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

18 Experts available now in Live!

Get 1:1 Help Now