Convert decimal, hexadecimal and binary string to integer in C++

1 Answer

0 votes
#include <iostream>
#include <string>

using std::cout;
using std::endl;
using std::string;

int main()
{
	string dec = "13 Street";
	string hex = "64C1";
	string bin = "1101111001";

	string::size_type sz;   

	int idec = stoi(dec, &sz);
	int ihex = stoi(hex, nullptr, 16);
	int ibin = stoi(bin, nullptr, 2);

	cout << dec << ": " << idec << endl;
	cout << hex << ": " << ihex << endl;
	cout << bin << ": " << ibin << endl;

	return 0;
}

/*
run:

13 Street: 13
64C1: 25793
1101111001: 889

*/

 



answered May 27, 2018 by avibootz

Related questions

1 answer 177 views
1 answer 181 views
1 answer 177 views
1 answer 202 views
1 answer 137 views
137 views asked Sep 17, 2021 by avibootz
...