0

*NOTE: there is a similar question on here but it did not help me because my friend straight up copy pasted it on to her code and I don't want to do that (plus it made in zero way any sense to me personally so why would I so much as refer to it in the first place if I don't follow the method used for it). I need some help with the LOGIC essentially, not even exactly the code written out line by line.

I have a function for my char to decimal already:

    void CharToDec(char hexVal)

   { 
     if(hexVal >= '0' && hexVal <= '9')

       hexVal = hexVal - '0';

     else

    {

      hexVal = hexVal - 'A' + 10;

    } 

   }

Not sure if those lines are right but it's what I got from my professor. I need to get a decimal conversion from hexadecimal using recursion. No exception. Can anyone please give me a pseudo-code to follow or a logic stream for dummies? I'm not smart nor do I understand recursion at all. Professor skimmed and sincerely didn't sound interested at all in helping us deal with this. Plus we don't have a textbook. And since the whole school is closed due to the pandemic, I am having an extra hard time to reach out to him.

My base switch code has to be a different function that's recursive. I'm using this block of code just to convert the characters into decimals in the hex chain.

Example output:

Enter hex value: 7F

Decimal value: 127

Like so.

Thank you.

May Athena
  • 39
  • 8
  • [how to convert hex to decimal recursively](https://stackoverflow.com/questions/56763669/how-to-convert-hexadecimal-to-decimal-recursively) – nick Mar 24 '20 at 06:56

1 Answers1

0

Please see the below code. Only a minimal version is added . You can customize this code based on your requirement

#include <iostream>

#include <string.h>

using namespace std;

void CharToDec(char hexVal[]) {
  int len = strlen(hexVal);

  // Initializing base value to 1, i.e 16^0 
  int base = 1;

  int dec_val = 0;

  // Extracting characters as digits from last character 
  for (int i = len - 1; i >= 0; i--) {
    // if character lies in '0'-'9', converting  
    // it to integral 0-9 by subtracting 48 from 
    // ASCII value. 
    if (hexVal[i] >= '0' && hexVal[i] <= '9') {
      dec_val += (hexVal[i] - 48) * base;

      // incrementing base by power 
      base = base * 16;
    }

    // if character lies in 'A'-'F' , converting  
    // it to integral 10 - 15 by subtracting 55  
    // from ASCII value 
    else if (hexVal[i] >= 'A' && hexVal[i] <= 'F') {
      dec_val += (hexVal[i] - 55) * base;
      // incrementing base by power 
      base = base * 16;
    }
  }
  cout << "Decimal value=" << dec_val << endl;
}

int main() {
  CharToDec("7F");
  return 0;
}

The output will be

Decimal value=127
Vishnu CS
  • 748
  • 1
  • 10
  • 24
  • 1
    Recursion is a requirement of the post title. – 2785528 Mar 29 '20 at 01:41
  • My dude, I would love for nothing more than to do this nonrecursively but I have to use recursion. It would be super easy for me to do it without recursion honestly. But I have a hard time following with recursion especially bc I have ADHD and can't follow through with anything really. Much less recursive logic. – May Athena Mar 30 '20 at 00:24