I have been trying to figure this out for a while now, is there any way to convert from a Hex string to an integer value in C++?
For example:
string hexstring = "100C";
needs to be converted to the integer equvalent.
I know how to convert a hex to int but not string hex to int.
But...I would really rather have it just be used as a hex value if there is someway to convert this string to a hex number that would be best. Because what I really need to do is to mimic some memory with hexadecimal values. I'm using an array to mimic memory, is there any way I can use hex values as indexes to places in my array? Anyways, if anyone can help me on this I would greatly appreciate it. Thanks.
How to convert a Hex string to an integer value in C++?
There are a number of ways to do this, one I am suggesting may or may not be one of the most optimal but is easy:
#include %26lt;iostream%26gt;
#include %26lt;string%26gt;
using namespace std;
int main()
{
string hex = "100C";
int decimalValue = 0;
sscanf(hex.c_str(), "%x", %26amp;decimalValue);
cout%26lt;%26lt;decimalValue%26lt;%26lt;endl;
return 0;
}
Else you know the algo and that hex is base 16, write your own function.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment