Answers for "convert hexadecimal to integer in c++"

C++
2

string hex to int c++

//C++11
std::string s = "0xfffefffe";
unsigned int x = std::stoul(s, nullptr, 16);
Posted by: Guest on April-18-2021
-1

convert from hex to decimal c++

#include <iostream>
#include <string>
#include "math.h"
using namespace std;
unsigned long hex2dec(string hex)
{
    unsigned long result = 0;
    for (int i=0; i<hex.length(); i++) {
        if (hex[i]>=48 && hex[i]<=57)
        {
            result += (hex[i]-48)*pow(16,hex.length()-i-1);
        } else if (hex[i]>=65 && hex[i]<=70) {
            result += (hex[i]-55)*pow(16,hex.length( )-i-1);
        } else if (hex[i]>=97 && hex[i]<=102) {
            result += (hex[i]-87)*pow(16,hex.length()-i-1);
        }
    }
    return result;
}

int main(int argc, const char * argv[]) {
    string hex_str;
    cin >> hex_str;
    cout << hex2dec(hex_str) << endl;
    return 0;
}
Posted by: Guest on January-01-2022

Code answers related to "convert hexadecimal to integer in c++"

Browse Popular Code Answers by Language