HELP ... hex to decimal conversion

1
2
3
4
5
6
7
8
9
10
11
12
//hex to decimal and bianary
	char hex;

	const string myHexString = hex;
	
	char *line = "short line for testing";
	long strtol( const char *nptr, char **endptr, int base );
	cout<<strtol(myHexString,NULL,16);
	//int x = (int)c2;
	cout << "int HEX: " << dec << x << endl;
	system ("pause");
	return 0;

this is my fricken code ... ive been working on this program for 2 weeks and this is the only problemn other then this the whole program works
I'm guessing that the strtol() is giving you problem. Try this:

1
2
3

cout << strtol( myHexString.data(), NULL, 16 );
This would be better as

cout << strtol( myHexString.c_str(), NULL, 16 );

while both data() and c_str() return a const char*, only the latter is required to ensure it's a pointer to a null terminated string.

http://www.cplusplus.com/reference/string/string/c_str/
http://www.cplusplus.com/reference/string/string/data/

thanks ill try both sugestons ill let you know if it works im in a seminar at the moment and i might get a couple minuites to throw it on my flash drive when i get back so i can take it home and try it. i will let you know thanks for the advice cuz after 3 weeks now im tired of debugging this and its the only problem
line 7 is what seems to be the problemn it says that there are problemns with strtlol is there maybe a library that i need to include ... this is just a snippit of code the rest of my code is unrelated tho
this is my fricken code

I lol'd
what problem?
Are you not over complicating it or am i missing the point?
1) enter a number in hex format
cin>>n;//n is entered as b00b
2)cout<<dec<<n<<endl;//prints 45067
....so why enter the hex numeral as a string is what i'm asking?
Topic archived. No new replies allowed.