Skip to content

Instantly share code, notes, and snippets.

@amitkumarj441
Last active August 3, 2016 19:33
Show Gist options
  • Save amitkumarj441/6dd1029c30a1b0b4fe13e8f0757c34ee to your computer and use it in GitHub Desktop.
Save amitkumarj441/6dd1029c30a1b0b4fe13e8f0757c34ee to your computer and use it in GitHub Desktop.
Program in C++ to convert string to number.
// 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