Click here to Skip to main content
15,789,700 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Trying to create a program that would convert a decimal into a hexadecimal string without using std::hex. When conversion occurs, it returns a flipped version of the correct answer for the Hexadecimal

For e.g : 100(decimal) converted to 46 instead of 64(correct hexadecimal).

Am unable to think of a solution for this problem.

int i;
int temp;
string hex;
long totalDecimal;
cin >> totalDecimal;

for (i = 0; totalDecimal != 0; i++)
{
    temp = totalDecimal % 16;

    if (temp > 10)
    {
        hex += temp + 55;
    }
    else
    {
        hex += temp + 48;
    }
    totalDecimal = totalDecimal / 16;
}

cout << hex;
return 0;


What I have tried:

making hex store an array e.g hex[100] and using for loops to assign values to each element in the array, but it only made it much more confusing and complicated
Posted
Updated 10-Oct-20 17:07pm

You have two options:
1) Store the hex digits and display them in reverse later.
2) Work from the most significant digit first.

Option2 isn't as bad as it sounds, not realy.
1) Set up a boolean called "leadingZero" and set it to true
2) Set up a shift count, set it to 28 (for 32 bit numbers, 60 for 64 bit)
3) Loop though all digits.
3.1) Shift the input by shift count bits, using the >> operator and mask it to a digit:
C++
digit = (input >> shiftCount) & 0xF;

3.2) If the digit not zero or leadignZero is false print the digit. Set leading Zero to false
3.3) Reduce shiftCount by 4 and loop back to 3.1
 
Share this answer
 
Comments
Member 13899738 5-Jul-18 9:00am    
I appreciate the 2 different options you gave! However, I am trying to make the function return the hexadecimal digits instead of printing them out in reverse. As for the second option, I have not learnt how bit shifting would work and preferably would not want to use it until I learn it. Thank you!!
Let's look at what happens:

Iteration 1:
totalDecimal = 100
totalDecimal%16 = 4
hex = "4"

Iteration 2:
totaldecimal = 6 (100/16)
totalDecimal%16 = 6
hex = "46"

So every iteration, you divide by 16 (going right to left), but you add to the end of your string (left to right).
You could fix it by adding to your string from right to left:

string hex = "";
...
    if (temp > 10)
    {
        hex.insert(0, 1, temp + 55);
    }
    else
    {
        hex.insert(0, 1, temp + 48);
    }
 
Share this answer
 
v3
Comments
Member 13899738 5-Jul-18 8:58am    
Thank you for the response! However, when I try to use hex = (temp + 55) + hex, the compiler states that there are "no operator '+' matches these operands". How can I resolve this issue?
[no name] 5-Jul-18 9:00am    
You're right, it's been a while since I typed C++ :) Please see my edit.
megaadam 5-Jul-18 9:45am    
hex = char(temp + 55) + hex;
megaadam 5-Jul-18 9:50am    
or...
just do this at the end of your original solution.
hex = std::reverse(hex);
You also need #include <algorithm>
Assuming you want to print out a hex string of the decimal number I default to using sprintf() to stuff a char array with the hex number using the %x specifier.

This is the old way and probably not what you superiors are after out of you but it will get you what you want - I think.
 
Share this answer
 
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int main()
{
  string hex;
  unsigned long l;
  cin >> l;

  if ( l == 0 )
    hex = "0";
  else
  {
    while (l)
    {
      unsigned char c = l & 0x0F;
      hex += static_cast<char>( c < 10 ? c + '0' : c - 10 + 'A' );
      l >>= 4;
    }
    reverse( hex.begin(), hex.end());
  }
  cout << hex << endl;
}
 
Share this answer
 
/*  decimal /hexa
     Jose Anilto
 */
 #include <iostream>
 #include <cstring>


 using namespace std;


 string converte(int n, string x) {

     string hexa = {"0123456789ABCDEF"};
     return (n < 16 ? "0x" + (hexa[n] + x) : converte(n/16, hexa[n%16] + x));

 }

 int main () {

     int nd ;
     string y = "";
     cout << "\n Digite um numero: "; cin >> nd;
     cout << "\n Numero digitado: " << nd << "\n";

     cout << "\n Hexadecimal: " << converte(nd, y) << "\n";

  return 0;
 }
 
Share this answer
 
v2
Comments
CHill60 12-Oct-20 9:15am    
You might want to remove your email address from a plain text open website - unless you like spam.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900