Last active
August 3, 2016 19:33
-
-
Save amitkumarj441/6dd1029c30a1b0b4fe13e8f0757c34ee to your computer and use it in GitHub Desktop.
Program in C++ to convert string to number.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Author: Amit Kumar Jaiswal | |
// Twitter: @amit_gkp | |
#include<iostream> | |
#include<string> | |
#include<cstdlib> | |
using namespace std; | |
long hex2int(const string& hexStr) | |
{ | |
char *offset; | |
if(hexStr.length()>2) | |
{ | |
if(hexStr[0]=='0' && hexStr[1]=='x') | |
{ | |
return strtol(hexStr.c_str(),&offset,0); | |
} | |
} | |
return strtol(hexStr.c_str(),&offset,16); | |
} | |
int main() | |
{ | |
string str1="0x12AB"; | |
cout<<hex2int(str1)<<endl; | |
string str2="12AB"; | |
cout<<hex2int(str2)<<endl; | |
string str3="QAFG"; | |
cout<<hex2int(str3)<<endl; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment