C++ hex2dec string function



#include <iostream>
#include <sstream>
#include <cstdio>
 
using namespace std;
 
unsigned long long hex2dec(const string &input);
unsigned long long hex2dec_c(const char*s, int length);
 
int main(int argc, char *argv[])
{
    string i1 = "FF";
    string i2 = "7FFFFFFF";
    string i3 = "FFFFFFFF";
    string i4 = "1004CCB00";//if hex2dec returned int, this would overflow it
 
    cout << i1 << "," << hex2dec(i1)<< "," << hex2dec_c(i1.c_str(),i1.length()) << endl;
    cout << i2 << "," << hex2dec(i2)<< "," << hex2dec_c(i2.c_str(),i2.length()) << endl;
    cout << i3 << "," << hex2dec(i3)<< "," << hex2dec_c(i3.c_str(),i3.length()) << endl;
    cout << i4 << "," << hex2dec(i4)<< "," << hex2dec_c(i4.c_str(),i4.length()) << endl;
 
    return 0;
}
 
unsigned long long hex2dec(const string &input)
{
    unsigned long long n;
    stringstream ss;
    ss << hex << uppercase << input;
    //ss << hex << lowercase << input;
    ss >> n;
    return n;
}
 
unsigned long long hex2dec_c(const char*s, int length)
{
    unsigned long long n=0;
    for(int i=0,v=0; i<length && s[i]!='\0'; i++)
    {
        int v;
        if      ('a' <= s[i] && s[i] <='f') { v=s[i]-97+10; }
        else if ('A' <= s[i] && s[i] <='F') { v=s[i]-65+10; }
        else if ('0' <= s[i] && s[i] <='9') { v=s[i]-48;    }
        else break;
        n*=16;
        n+=v;
    }
    return n;
}
FF,255,255
7FFFFFFF,2147483647,2147483647
FFFFFFFF,4294967295,4294967295
1004CCB00,4300000000,4300000000