Decimal to Hexidecimal

The only problem I'm having is that it's in reverse order. I can only use the libraries listed in the code. Do I need to create an array to store each character? But some are ints and some are strings. How do I do this?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
#include <string>
using namespace std;

int main() {
    cout << "Enter a decimal number: " << endl;
    int num;
    int remainder;
    cin >> num; // user inputs decimal number
    cout << 0 << "x";
    
    while (num>0) {
        remainder = num % 16;
        if (remainder < 10) {
            cout << remainder;
        }
        else {
           char c = static_cast<char>(remainder-10+'A');
           string s = string (1,c);
           cout << s;
        }
        num /= 16;
    }
    cout << endl;
}
How exactly would I apply this? Sorry, complete noob here.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
    std::string hex{};
    while (num>0) {
        remainder = num % 16;
        if (remainder < 10) {
            //cout << remainder;
            hex += to_string( remainder );
        }
        else {
           char c = static_cast<char>(remainder-10+'A');
           string s = string (1,c);
           //cout << s;
           hex += s;
        }
        num /= 16;
    }
    hex = string{ hex.rbegin(), hex.rend() };
    cout << "0x" << hex << '\n';


Enter a decimal number: 100
0x64


Last edited on
Thanks, but I can't use the hex feature...
Last edited on
Thanks, but I can't use the hex feature...
std::string hex{};
I think you're referring to the std::hex manipulator, but I've used a std::string called hex.
I can only use the libraries listed in the code.


Here. While this is unacceptable, it's close: I'm posting it here in the hope that it will be helpful.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# include <iostream>
# include <cmath>

int main (int, char **) {
  /* symbol table for place values. */
  static char constexpr sym[36] = {
    '0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F',
    'G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V',
    'W','X','Y','Z'
  };

  /* Convert this value */
  unsigned const N = 12941; // 12941 = 0x328D
  /* ...to radix 16.   Constraint: 1 < rx <= sizeof(sym) */
  uint64_t constexpr rx = 16;
  for (int place = floor(log(N) / log(rx)); place >= 0; place--)
    std::cout << sym[static_cast<unsigned int>(N / pow(rx, place)) % rx];
  std::cout << "\n";
}

If you choose to use this process you could implement your own floor(), log(), and a pow() for integers.
Duplicating floor() and pow() is simple, but implementing the natural logarithm is a good exercise on it's own.

Fortunately, while the logarithm is elegant, it's not required because it's only used to determine the number of places required to represent the converted value. This can be done instead with a lookup, given that the radix is always 16.
Last edited on
Topic archived. No new replies allowed.