0

I'm trying to use a C++ program to convert a hexadecimal value into a decimal value. Just can't come up with a working code.

This is the best thing I have come up with:

int main () {
    string link;
    string hex_code;
    int dec_code;
    int i;
    int n = 6;
    int num;
    int hex;

    cout << "Insert 1 of the HEX characters at a time:";

    for (i = 0; i < n; i++) {
        cin >> hex_code;
    }
    for (i = 0; i < n; i++) {
        if (hex_code == "A") {
            hex_code = 10;
        }
        else if (hex_code == "B") {
            hex_code = 11;
        }
        else if (hex_code == "C") {
            hex_code = 12;
        }
        else if (hex_code == "D") {
            hex_code = 13;
        }
        else if (hex_code == "E") {
            hex_code = 14;
        }
        else if (hex_code == "F") {
            hex_code = 15;
        }
        else {
            hex_code = hex_code;
        }
        num = hex * pow (16, i);
    }
    for (i = 0; i < n; i++) {
        dec_code = dec_code + num;
    }
    cout << dec_code;
return 0;
}

Any help/feddback/opinions are welcome.

Edit: Thank you for all your help. Found the code I tryed to create, but failed, here: https://stackoverflow.com/a/27334556/13615474

Tine Reiu
  • 11
  • 2

3 Answers3

0

There is a hex manipulator in iomanip library of C++

   cout << "Insert 1 of the HEX characters at a time:";

    for (i = 0; i < n; i++) {
        int hexcode;
        std::cin >> std::hex >> hexcode;
        std::cout << hexcode << std::endl;
    }

This would print decimal equivalent of given hex code

Abhishek Bhagate
  • 5,583
  • 3
  • 15
  • 32
0

Hex to decimal conversion can be performed by reading the input number as a character array and performing the conversion arithmetic on each character.

Here is a working example for converting hexadecimal into decimal:

// File name: HexToDec.cpp    
#include <iostream>
#include <cstring>
using namespace std;

int hexToDec(char hexNumber[]) {
    int decimalNumber = 0;
    int len = strlen(hexNumber);
    for (int base = 1, i=(len-1); i>=0; i--, base *= 16) {
       // Get the hex digit in upper case 
       char digit = toupper(hexNumber[i]);
       if ( digit >= '0' && digit <='9' ) {
          decimalNumber += (digit - 48)*base;
       }
       else if ( digit >='A' && digit <='F' ) {
          decimalNumber += (digit - 55)*base;
       }
    }
    return decimalNumber;
}

int main() {

   char hexNumber[80];
   // Read the hexadecimal number as a character array
   cout << "Enter hexadecimal number: ";
   cin >> hexNumber;
   cout << hexNumber << " in decimal format = " << hexToDec(hexNumber) << "\n";
   return 0;
}

Output:

Enter hexadecimal number: DEC
DEC in decimal format = 3564

More information:

https://www.geeksforgeeks.org/program-for-hexadecimal-to-decimal/

Gopinath
  • 4,066
  • 1
  • 14
  • 16
0

Here is a simple code fragment using a lookup table:

char c;
static const char hex_to_decimal[] = "0123456789ABCDEF";
std::cin >> c;
int decimal = 0;
for (decimal = 0; decimal < sizeof(hex_to_decimal) - 1; ++decimal)
{
  if (hex_to_decimal[i] == c)
  {
      break;
  }
}

Another conversion method:

std::cin >> c;
int decimal;
if (is_digit(c))
{
    decimal = c - '0';
}
else
{
    decimal = 10 + c - 'A';
}

The above code fragment assumes that the encoding has 'A'...'F' contiguous. The first example is more portable.

Thomas Matthews
  • 56,849
  • 17
  • 98
  • 154