By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,203 Members | 1,638 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,203 IT Pros & Developers. It's quick & easy.

Unreversing hexadecimal conversion?

P: 5
Ok so I am converting to hexadecimal from decimal and I can only cout the reverse order of the hexadecimal?! How could I reverse this so its the right order? Heres my code:

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. using namespace std;
  3. void binary(int number) 
  4. {
  5.     int remainder;
  6.  
  7.     if(number <= 1) 
  8.     {
  9.         cout << number;
  10.         return;
  11.     }
  12.  
  13.     remainder = number%2;
  14.     binary(number >> 1);    
  15.     cout << remainder;
  16. }
  17.  
  18. void hexadecimal(int number)
  19. {
  20.     int remainder;
  21.     while (number >= 1)
  22.     {
  23.         remainder = number%16;
  24.         if (remainder > 9)
  25.         {
  26.             if (remainder == 10)
  27.                 cout << "A";
  28.             if (remainder == 11)
  29.                 cout << "B";
  30.             if (remainder == 12)
  31.                 cout << "C";
  32.             if (remainder == 13)
  33.                 cout << "D";
  34.             if (remainder == 14)
  35.                 cout << "E";
  36.             if (remainder == 15)
  37.                 cout << "F";
  38.         }
  39.         else
  40.         {
  41.             cout << remainder;
  42.         }
  43.         number = number/16;
  44.     }
  45. };
  46. void octal(int number)
  47. {
  48.     int remainder;
  49.     int i;
  50.     int ten = 10;
  51.     remainder = number % 8;
  52.     number = number/8;
  53.     i = remainder;
  54.     while (number >= 1)
  55.     {
  56.         remainder = number%8;
  57.         number = number/8;
  58.         i += remainder * ten;
  59.         ten = ten * 10;
  60.     }
  61.     cout << i << "\n";
  62. };
  63. int main ()
  64. {
  65.     int number;
  66.     cout << "Enter a number in decimal: ";
  67.     cin >> number;
  68.     int spec;
  69.     cout << "Specify whether you want to convert to Binary, Octal, or Hexadecimal (1-3): ";
  70.     cin >> spec;
  71.     if (spec == 1)
  72.     {
  73.         binary (number);
  74.         cout << "\n";
  75.     }
  76.     if (spec == 2)
  77.         octal (number);
  78.     if (spec == 3)
  79.         hexadecimal (number);
  80.     return 0;
  81. }
  82.  
Jul 27 '08 #1
Share this Question
Share on Google+
14 Replies


Banfa
Expert Mod 5K+
P: 8,959
Instead of outputting to cout output to a string stream, then when you have finished reverse the string and output the string.

In fact better still don't output to cout in you function at all, have you function create and return a string and output the string from you main function.
Jul 27 '08 #2

Expert 10K+
P: 11,448
I don't know why this thread was closed; another solution would be to simply
reverse the recursive steps like this:

Expand|Select|Wrap|Line Numbers
  1. void binary(int x) {
  2.    if (x > 1) binary(x>>1);
  3.    cout << (x&1);
  4. }
  5.  
Instead of applying right (or 'tail') recursive steps use left recursion where you
test for the sentinel condition first.

kind regards,

Jos
Jul 27 '08 #3

Banfa
Expert Mod 5K+
P: 8,959
I don't know why this thread was closed;
Sorry that was me, not enough coffee yet this morning, thank you for re-opening it.
Jul 27 '08 #4

Expert 10K+
P: 11,448
Sorry that was me, not enough coffee yet this morning, thank you for re-opening it.
No need to apologize, I know that feeling; my recipe: two cups of original Italian
espresso coffee in the morning, that'll reduce the amount of blood in the coffee veins.

kind regards,

Jos ;-)
Jul 27 '08 #5

P: 5
Ok so I have a decimal to hexadecimal converter and I want to use a stack class to reverse the characters I get into the proper order...

Here's the code I have for the converter:
Expand|Select|Wrap|Line Numbers
  1. void hexadecimal(int number)
  2. {
  3.     int remainder;
  4.     while (number > 0)
  5.     {
  6.         remainder = number % 16;
  7.         number = number/16;
  8.         if (remainder > 9)
  9.             cout << char('A'  + remainder - 10) ;
  10.         else cout << remainder;
  11.     }
  12. };
  13.  
How would I make a stack class to reverse the order? Should I store the characters I get in an array? How would I do that?

Thanks for all help,

Greg
Jul 29 '08 #6

Savage
Expert 100+
P: 1,764
You could use STL's string for this. Instead of using cout use string's += operator to add a new char to the string.After that you can obtain string reverse iterator and print the string in reverse order.

Take a look at this for reference to how to reverse a string.
Jul 29 '08 #7

Banfa
Expert Mod 5K+
P: 8,959
Please do not start multiple threads on the same topic, it is against the site posting guidelines, please read them.

Banfa
Administrator
Jul 30 '08 #8

Expert 10K+
P: 11,448
Did anyone actually *read* my reply #3? There is no need to reverse anything
at the end.

kind regards,

Jos
Jul 30 '08 #9

Banfa
Expert Mod 5K+
P: 8,959
Did anyone actually *read* my reply #3? There is no need to reverse anything at the end.
I did Jos but it is re-working the binary function and unless I missed something is actually functionally equivalent to what is already there although more elegant, however the OP asked for help in the hexadecimal function.
Jul 30 '08 #10

Expert 10K+
P: 11,448
I did Jos but it is re-working the binary function and unless I missed something is actually functionally equivalent to what is already there although more elegant, however the OP asked for help in the hexadecimal function.
I don't understand that; there is nothing structurally different between a binary()
or hexadecimal() function; both can use that left recursion (see reply #3) so
nothing needs to be reversed. Basically they all go:

if (n > radix) recurse(n/radix);
print representation of n%radix;

kind regards,

Jos
Jul 30 '08 #11

Banfa
Expert Mod 5K+
P: 8,959
I don't understand that; there is nothing structurally different between a binary()
or hexadecimal() function; both can use that left recursion (see reply #3) so
nothing needs to be reversed. Basically they all go:

if (n > radix) recurse(n/radix);
print representation of n%radix;
I agree entirely in principle but that isn't how the hexadecimal function was written initially. My comments suggest how to fix it without introducing recursion based on what was already there.

Actually now I think about it a recursion of this sort is probably one I would be happy to put into a live project.

My normal worry with recursion is preventing or not risking stack overflow which can be troublesome if there is no well defined boundary to the limit of recursion. For a solution to this particular problem the boundary is very well defined because of the limiting fact in the maximum value an unsigned integer can hold.
Jul 30 '08 #12

Expert 10K+
P: 11,448
I agree entirely in principle but that isn't how the hexadecimal function was written initially. My comments suggest how to fix it without introducing recursion based on what was already there.

Actually now I think about it a recursion of this sort is probably one I would be happy to put into a live project.

My normal worry with recursion is preventing or not risking stack overflow which can be troublesome if there is no well defined boundary to the limit of recursion. For a solution to this particular problem the boundary is very well defined because of the limiting fact in the maximum value an unsigned integer can hold.
I had only read the OP's binary() function that was written recursively already and
assumed that the other methods would've been written identically, hence my
answer.

This is for the OP: even one (recursive) function can do it all:

Expand|Select|Wrap|Line Numbers
  1. void convert(unsigned int n, int radix) {
  2.    if (n >= radix) convert(n/radix, radix);
  3.    cout << "0123456789ABCDEF"[n%radix];
  4. }
  5.  
kind regards,

Jos
Jul 30 '08 #13

P: 5
Sorry I thought my old post was closed or something :/

I'm trying to do this with a stack but its not working for some reason what am I doing wrong?

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. using namespace std;
  3. class stack
  4. {
  5. public:
  6.     stack () {stackSize = 0;};
  7.     ~stack() {}
  8.     void push(char i)
  9.     {
  10.         stackChar[stackSize++] = i;
  11.     }    
  12.     char pop()
  13.     {
  14.         return stackChar[--stackSize];
  15.     }
  16.     bool isEmpty ()
  17.     {
  18.         if (stackSize = 0)
  19.             return true;
  20.         else
  21.             return false;
  22.     }
  23. private:
  24.     char stackChar[100];
  25.     int stackSize;
  26. };
  27. void binary(int number) 
  28. {
  29.     int remainder;
  30.  
  31.     if(number <= 1) 
  32.     {
  33.         cout << number;
  34.         return;
  35.     }
  36.  
  37.     remainder = number%2;
  38.     binary(number >> 1);    
  39.     cout << remainder;
  40. }
  41.  
  42. void hexadecimal(int number)
  43. {
  44.     stack hex;
  45.     int remainder;
  46.     while (number > 0)
  47.     {
  48.         remainder = number % 16;
  49.         number = number/16;
  50.         if (remainder > 9)
  51.             hex.push('A'  + remainder - 10) ;
  52.         else hex.push(remainder);
  53.     }
  54.     hex.pop();
  55. };
  56. void octal(int number)
  57. {
  58.     int remainder;
  59.     int i;
  60.     int ten = 10;
  61.     remainder = number % 8;
  62.     number = number/8;
  63.     i = remainder;
  64.     while (number >= 1)
  65.     {
  66.         remainder = number%8;
  67.         number = number/8;
  68.         i += remainder * ten;
  69.         ten = ten * 10;
  70.     }
  71.     cout << i << "\n";
  72. };
  73. int main ()
  74. {
  75.     int number;
  76.     cout << "Enter a number in decimal: ";
  77.     cin >> number;
  78.     int spec;
  79.     cout << "Specify whether you want to convert to Binary, Octal, or Hexadecimal (1-3): ";
  80.     cin >> spec;
  81.     if (spec == 1)
  82.     {
  83.         binary (number);
  84.         cout << "\n";
  85.     }
  86.     if (spec == 2)
  87.         octal (number);
  88.     if (spec == 3)
  89.         hexadecimal (number);
  90.     return 0;
  91. }
  92.  
Jul 30 '08 #14

boxfish
Expert 100+
P: 469
I'm trying to do this with a stack but its not working for some reason what am I doing wrong?
Not much. It almost works, but:
In your hexidecimal function,
Expand|Select|Wrap|Line Numbers
  1. if (remainder > 9)
  2.     hex.push('A'  + remainder - 10);
  3. else hex.push(remainder);
hex.push(remainder); is not right, because the int value 0 does not convert to the char value '0'. Do the same thing you did for letters: hex.push('0' + remainder);

There is something very very wrong with your isEmpty function: a missing = sign. It says
Expand|Select|Wrap|Line Numbers
  1. if (stackSize = 0)
, but it should say
Expand|Select|Wrap|Line Numbers
  1. if (stackSize == 0)
. Very important difference between assignment and equality testing.

And finally, you might already know this, but you aren't printing your finished hexidecimal number. I'm guessing you're planning to change the hex.pop(); at the end of your hexidecimal function into a loop that pops characters off and prints them.

Hope this helps.
Jul 31 '08 #15

Post your reply

Sign in to post your reply or Sign up for a free account.