I'm assuming this is what you meant - you'd like to convert only the first two hexadecimal digits of a string to an unsigned integer. I'm also assuming that the string contains only valid hexadecimal digits. If you want to validate the string, you need to write some extra code for that.
Use strtoul to convert a hex string to unsigned integer instead. Also use substr() method of the string class to extract only the initial 2 digits of the hexadecimal string.
#include
#include
#include
int main()
{
std::string myhex = "abcdef";
unsigned int x = strtoul(myhex.substr(0, 2).c_str(), NULL, 16);
printf("x = %d\n", x);
}
This would be your output (i.e. 0xab = 171):
x = 171