#include <iostream>
#include <string>
#include <stdio.h>
using namespace std;
bool is_hex(char c, int &v)
{
if (0x20 <= c && isdigit(c))
{
v = c - '0';
return true;
}
else if ('A' <= c && c <= 'F')
{
v = c - 'A' + 10;
return true;
}
else if ('a' <= c && c <= 'f')
{
v = c - 'a' + 10;
return true;
}
return false;
}
bool from_hex_to_i(const std::string &s, size_t i, size_t cnt, int &val)
{
if (i >= s.size())
{
return false;
}
val = 0;
for (; cnt; i++, cnt--)
{
if (!s[i])
{
return false;
}
int v = 0;
if (is_hex(s[i], v))
{
val = val * 16 + v;
}
else
{
return false;
}
}
return true;
}
int main()
{
string str = "123";
int val = 0;
size_t len = str.size();
bool ret = from_hex_to_i(str, 0, len, val);
printf("ret = %d\n", ret);
printf("value = %d\n", val);
return 0;
}