Search
 
SCRIPT & CODE EXAMPLE
 

CPP

Convert a hexadecimal number into decimal c++

#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
    int x;
    cin >>hex >> x;
    cout << x << endl;
    return 0;
}
Comment

how to convert hexadecimal to decimal in c++

#include<bits/stdc++.h>
using namespace std;

int hexadecimaltoDecimal(string n)
{
    int ans = 0;
    int x = 1;
    int s = n.size();
    for( int i=s-1 ; i>=0 ; i-- )
    {
        if( n[i] >= '0' && n[i] <= '9' )
        {
            ans += x*( n[i] - '0' );
        }
        else if( n[i] >= 'A' && n[i] <= 'F' )
        {
            ans += x*( n[i] - 'A' + 10 );
        }
        x *= 16;
    }
return ans;
}

int main()
{
    string n;
    cout<<"Input a Hexa Decimal number :"<<endl;
    cin>>n;
    cout<<"The Decimal number will be :"<<endl;
    cout<<hexadecimaltoDecimal(n)<<endl;
return 0;
}
Comment

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;
}
Comment

PREVIOUS NEXT
Code Example
Cpp :: how to remove the scroll bar in pyqt6 
Cpp :: sweetalert2 email and password 
Cpp :: c++ open webpage 
Cpp :: cpp oop 
Cpp :: cpp ignore warning in line 
Cpp :: c++ count vector elements 
Cpp :: pop off end of string c++ 
Cpp :: adding variables c++ 
Cpp :: glfw error: the glfw library is not initialized 
Cpp :: ue4 int to enum c++ 
Cpp :: how to use for c++ 
Cpp :: find maximum sum in array of contiguous subarrays 
Cpp :: vectors in c++ 
Cpp :: balanced brackets in c++ 
Cpp :: c++ convert to assembly language 
Cpp :: c++ define array with values 
Cpp :: c++ memory address 
Cpp :: minimum characters to make string palindrome 
Cpp :: convert single character string to char c++ 
Cpp :: 1. Two Sum 
Cpp :: c++ unordered_map initialize new value 
Cpp :: max and min function in c++ 
Cpp :: cout in c++ 
Cpp :: fname from FString 
Cpp :: c++ throw index out of bound 
Cpp :: OpenCV" is considered to be NOT FOUND 
Cpp :: how does sorting array works in c++ 
Cpp :: c++ optimize big int array 
Cpp :: c++ localtime unsafe 
Cpp :: Opengl GLFW basic window 
ADD CONTENT
Topic
Content
Source link
Name
4+9 =