Home > database >  Decimal to hexadecimal conversion program not working?? Don't know why?
Decimal to hexadecimal conversion program not working?? Don't know why?

Time:10-12

below is c program for decimal to hexadecimal conversion!!

// decimal to hexadecimal conversion
#include <iostream>
#include <math.h>
#include <string>
using namespace std;
string d2h(int n) {
  string s, t;
  int i = 0;
  while (n >= 1) {
    t[i] = n % 16;
    switch (t[i]) {
      case 10:
        t[i] = 'A';
        break;
      case 11:
        t[i] = 'B';
        break;
      case 12:
        t[i] = 'C';
        break;
      case 13:
        t[i] = 'D';
        break;
      case 14:
        t[i] = 'E';
        break;
      case 15:
        t[i] = 'F';
        break;
      default:
        break;
    }
    s[i] = t[i];
    n /= 16;
    i  ;
  }
  return s;
}
int main() {
  int n;
  cin >> n;
  cout << d2h(n);
  return 0;
}

I am getting nothing as output!!

PS C:\Users\anmol\Desktop\c  projectwork> g   .\dec2hexadec.cpp
PS C:\Users\anmol\Desktop\c  projectwork> ./a
479
PS C:\Users\anmol\Desktop\c  projectwork>

What should i change??

CodePudding user response:

  1. You are not allocating space in s or t for memory. Use s.push_back() instead of s[i].

  2. You'll probably need to reverse the string as well.

  3. t[i] = n % 16; This does not assign the character '0', but rather the number 0. Why don't you just create 16 cases in your switch statement? Or use the ASCII quirk '0' (n % 16).

CodePudding user response:

// decimal to hexadecimal conversion
#include <iostream>
#include <math.h>
#include <string>
using namespace std;

string d2h(int n) {
    string s, t;
    string Sign;
    int i = 0;
    // n is equal to 0? <0 or >0?
    if (n == 0){
        
        return "0";
    }
    if (n < 0){
        Sign = "-";
        n *= -1;
    }
    
    while (n ) {
        t.push_back(n % 16) ;
        switch (t[i]) {
        case 0:
            t[i] = '0';
            break;
        case 1:
            t[i] = '1';
            break;
        case 2:
            t[i] = '2';
            break;
        case 3:
            t[i] = '3';
            break;
        case 4:
            t[i] = '4';
            break;
        case 5:
            t[i] = '5';
            break;
        case 6:
            t[i] = '6';
            break;
        case 7:
            t[i] = '7';
            break;
        case 8:
            t[i] = '8';
            break;
        case 9:
            t[i] = '9';
            break;
        case 10:
            t[i] = 'A';
            break;
        case 11:
            t[i] = 'B';
            break;
        case 12:
            t[i] = 'C';
            break;
        case 13:
            t[i] = 'D';
            break;
        case 14:
            t[i] = 'E';
            break;
        case 15:
            t[i] = 'F';
            break;
        default:
            break;
        }
        s.push_back(t[i]);
      
        n /= 16;
        i  ;
    }
    reverse(s.begin(), s.end());
    return Sign s;
}
int main() {
    int n;
    cin >> n;
    cout << d2h(n);
    return 0;
}
  • Related