Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Convert string to integer

Posted on 2014-04-07
10
Medium Priority
?
246 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
7 Comments
 
LVL 71

Assisted Solution

by:Qlemo
Qlemo earned 1000 total points
ID: 39984038
atoi / atof should have a wide string sibling
0
 
LVL 86

Accepted Solution

by:
jkr earned 1000 total points
ID: 39984523
'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
ID: 39985062
What about localization? Will strtol and stringstream handle localization properly. What if decimal separtor is "," instead of ".".
0
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
LVL 86

Expert Comment

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

Expert Comment

by:jkr
ID: 39990301
Do you still have issues with this or have you just not thought of closing the question?
0
 
LVL 9

Expert Comment

by:Subrat (C++ windows/Linux)
ID: 40020059
// 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
ID: 40021727
Um, integers won't have a double-type separator such as a comma or a semicolon in the 1st place ;o)
0

Featured Post

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.

Question has a verified solution.

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

MS Access 2003 or later To MySQL Migration Project Hello All, this is my second article in the category of MS-OFFICE Automation. In internet I am not able to find any comprehensive resource on the Migration of MS Access back-end to MySQL so I fin…
User Beware!  This is a rather permanent solution to removing your email from an exchange server.  The only way to truly go back is to have your exchange administrator restore your mailbox from backups.  This is usually the option of last resort.  A…
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.
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.

879 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