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: -
#include <iostream>
-
using namespace std;
-
void binary(int number)
-
{
-
int remainder;
-
-
if(number <= 1)
-
{
-
cout << number;
-
return;
-
}
-
-
remainder = number%2;
-
binary(number >> 1);
-
cout << remainder;
-
}
-
-
void hexadecimal(int number)
-
{
-
int remainder;
-
while (number >= 1)
-
{
-
remainder = number%16;
-
if (remainder > 9)
-
{
-
if (remainder == 10)
-
cout << "A";
-
if (remainder == 11)
-
cout << "B";
-
if (remainder == 12)
-
cout << "C";
-
if (remainder == 13)
-
cout << "D";
-
if (remainder == 14)
-
cout << "E";
-
if (remainder == 15)
-
cout << "F";
-
}
-
else
-
{
-
cout << remainder;
-
}
-
number = number/16;
-
}
-
};
-
void octal(int number)
-
{
-
int remainder;
-
int i;
-
int ten = 10;
-
remainder = number % 8;
-
number = number/8;
-
i = remainder;
-
while (number >= 1)
-
{
-
remainder = number%8;
-
number = number/8;
-
i += remainder * ten;
-
ten = ten * 10;
-
}
-
cout << i << "\n";
-
};
-
int main ()
-
{
-
int number;
-
cout << "Enter a number in decimal: ";
-
cin >> number;
-
int spec;
-
cout << "Specify whether you want to convert to Binary, Octal, or Hexadecimal (1-3): ";
-
cin >> spec;
-
if (spec == 1)
-
{
-
binary (number);
-
cout << "\n";
-
}
-
if (spec == 2)
-
octal (number);
-
if (spec == 3)
-
hexadecimal (number);
-
return 0;
-
}
-
14 3159 Banfa 9,065
Expert Mod 8TB
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.
I don't know why this thread was closed; another solution would be to simply
reverse the recursive steps like this: -
void binary(int x) {
-
if (x > 1) binary(x>>1);
-
cout << (x&1);
-
}
-
Instead of applying right (or 'tail') recursive steps use left recursion where you
test for the sentinel condition first.
kind regards,
Jos
Banfa 9,065
Expert Mod 8TB
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.
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 ;-)
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: -
void hexadecimal(int number)
-
{
-
int remainder;
-
while (number > 0)
-
{
-
remainder = number % 16;
-
number = number/16;
-
if (remainder > 9)
-
cout << char('A' + remainder - 10) ;
-
else cout << remainder;
-
}
-
};
-
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
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.
Banfa 9,065
Expert Mod 8TB
Please do not start multiple threads on the same topic, it is against the site posting guidelines, please read them.
Banfa
Administrator
Did anyone actually *read* my reply #3? There is no need to reverse anything
at the end.
kind regards,
Jos
Banfa 9,065
Expert Mod 8TB
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.
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
Banfa 9,065
Expert Mod 8TB
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.
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: -
void convert(unsigned int n, int radix) {
-
if (n >= radix) convert(n/radix, radix);
-
cout << "0123456789ABCDEF"[n%radix];
-
}
-
kind regards,
Jos
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? -
#include <iostream>
-
using namespace std;
-
class stack
-
{
-
public:
-
stack () {stackSize = 0;};
-
~stack() {}
-
void push(char i)
-
{
-
stackChar[stackSize++] = i;
-
}
-
char pop()
-
{
-
return stackChar[--stackSize];
-
}
-
bool isEmpty ()
-
{
-
if (stackSize = 0)
-
return true;
-
else
-
return false;
-
}
-
private:
-
char stackChar[100];
-
int stackSize;
-
};
-
void binary(int number)
-
{
-
int remainder;
-
-
if(number <= 1)
-
{
-
cout << number;
-
return;
-
}
-
-
remainder = number%2;
-
binary(number >> 1);
-
cout << remainder;
-
}
-
-
void hexadecimal(int number)
-
{
-
stack hex;
-
int remainder;
-
while (number > 0)
-
{
-
remainder = number % 16;
-
number = number/16;
-
if (remainder > 9)
-
hex.push('A' + remainder - 10) ;
-
else hex.push(remainder);
-
}
-
hex.pop();
-
};
-
void octal(int number)
-
{
-
int remainder;
-
int i;
-
int ten = 10;
-
remainder = number % 8;
-
number = number/8;
-
i = remainder;
-
while (number >= 1)
-
{
-
remainder = number%8;
-
number = number/8;
-
i += remainder * ten;
-
ten = ten * 10;
-
}
-
cout << i << "\n";
-
};
-
int main ()
-
{
-
int number;
-
cout << "Enter a number in decimal: ";
-
cin >> number;
-
int spec;
-
cout << "Specify whether you want to convert to Binary, Octal, or Hexadecimal (1-3): ";
-
cin >> spec;
-
if (spec == 1)
-
{
-
binary (number);
-
cout << "\n";
-
}
-
if (spec == 2)
-
octal (number);
-
if (spec == 3)
-
hexadecimal (number);
-
return 0;
-
}
-
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, - if (remainder > 9)
-
hex.push('A' + remainder - 10);
-
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
, but it should say
. 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.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: pavithra.eswaran |
last post by:
Hi,
I would like to convert a single precision hexadecimal number to
floating point. The following program seems to work fine..
But I do not want to use scanf. I already have a 32 bit hexadecimal...
|
by: Damon |
last post by:
I'm getting '', hexadecimal value 0x02, is an invalid character when I'm
deseralizing XML from a 3rd party XML gateway. How do I get rid of these
hexadecimal values before I deserialize?
Cheers...
|
by: akash deep batra |
last post by:
hi
i want to convert a 96 bit binary number into a hexadecimal number.
e.g binary number= 001100010001010000100101011110111111010101110100010110000101011000101010000000000000000000000000
how...
|
by: Vijay |
last post by:
Hi ,
I am doing a small project in c.
I have a Hexadecimal file and want to convert into ascii value.
(i.e., Hexadecimal to Ascii conversion from a file).
Could anyone help me?
Thanks in...
|
by: elliotng.ee |
last post by:
I have a text file that contains a header 32-bit binary. For example,
the text file could be:
%%This is the input text
%%test.txt
Date: Tue Dec 26 14:03:35 2006...
|
by: Andrea |
last post by:
Hi,
suppose that I have a string that is an hexadecimal number, in order
to print this string I have to do:
void print_hex(unsigned char *bs, unsigned int n){
int i;
for (i=0;i<n;i++){...
|
by: sweeet_addiction16 |
last post by:
hello
Im writin a code in c...
can sum1 pls help me out in writing a c code to convert decimalnumber
to hexadecimal number.The hexadecimal number generated has to be an
unsigned long.
|
by: =?Utf-8?B?VHJlY2l1cw==?= |
last post by:
Hello, Newsgroupians:
I've a question regarding Hexadecimal to binary conversion.
Suppose I have a long hexadecimal string, and I would like to convert it to
a binary string. How can I...
|
by: i_robot73 |
last post by:
I have a file, containing hex values for dates (MMDDYYYY)<status
code><??such as:
...
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
by: Shællîpôpï 09 |
last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
|
by: af34tf |
last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
| |