### Thread: Convert hex to octal

1. #### Convert hex to octal(the display)

hi there i have a function that converts(its just the display) binary, octal and hexadecimal into a decimal value
Code:
```int convert2dec(int number, int base) {
int total = 0;
int power = 1;

while(number > 0) { //keeps on doing this as long as the number is more than 0

total += number % 10 * power;
number = number / 10;
power = power * base;
}

}```
what i want to ask if how would i go abt converting a hex to a decimal using this? lets say for example when the user enters 1A or 3F?

e.g hex 1B = dec 27

Thanks!! :D
Last edited by icepricessa; April 25th, 2007 at 10:10 AM.
2. How does it do that!? An integer is an integer is an integer.

Internally it is always binary, and externally is however you wish to represent it. You cannot hold a hexadecimal representation as anything other than a string, and your convert2dec() function does not take a string, it takes an integer. It also returns an int, which is not decimal either, it is only decimal if you choose to display a string representation of it in that way.

Clifford
3. Originally Posted by clifford
How does it do that!? An integer is an integer is an integer.

Internally it is always binary, and externally is however you wish to represent it. You cannot hold a hexadecimal representation as anything other than a string, and your convert2dec() function does not take a string, it takes an integer. It also returns an int, which is not decimal either, it is only decimal if you choose to display a string representation of it in that way.

Clifford
all its does is take an int value and manipulates its display? it doest really change the value of the int that is displayed. if a number entered was 11011 convert2dec(11011, 2) just will get the decimal conversion of it? kinda like in here
http://www.dreamincode.net/code/snippet585.htm
Last edited by icepricessa; April 25th, 2007 at 10:10 AM.
4. Yuck!

He has you thinking that you're entering a binary number, whereas it's actually reading it as a decimal. And then it goes through and changes that decimal value to a different decimal value? [shudder]

What happens when you try to enter a binary number of practical size? Like something the size of 32 bits? I think that program limits you to only 10 bits, because just one more bit would cause the input integer (a 32-bit integer nowadays) to overflow.

Why not just enter a string and convert that by parsing through each digit? That would also solve your issue of how to input hex numbers.

In short, I think that the approach you're taking is hosed and you need to try a different approach.

PS
Shift-and-add. Shifting means multiplying by the base.
Iterate through each character going from left to right.
Preset your result value to zero.
For each character, convert it to a numeric value. You would also test it at this point for being a valid digit, generating the appropriate error response. You would also test here for a termination character (ie, end of the string) whereupon you would exit.
Shift the result value (ie, multiply by the base).
Add the digit's value to the result.
Continue the iteration.

You should be able to handle any size of number, provided that its decimal value doesn't cause an overflow -- a 31-bit binary would be no problem, but for a 32-bit binary I think you would want to declare the result variable to be unsigned.
Last edited by dwise1_aol; April 25th, 2007 at 10:49 AM.
5. Originally Posted by dwise1_aol
Yuck!

He has you thinking that you're entering a binary number, whereas it's actually reading it as a decimal. And then it goes through and changes that decimal value to a different decimal value? [shudder]

What happens when you try to enter a binary number of practical size? Like something the size of 32 bits? I think that program limits you to only 10 bits, because just one more bit would cause the input integer (a 32-bit integer nowadays) to overflow.

Why not just enter a string and convert that by parsing through each digit? That would also solve your issue of how to input hex numbers.

In short, I think that the approach you're taking is hosed and you need to try a different approach.
hmm... the things is this program is supposed to get the value(int) input like b11011 is binary 11011 and convert2dec(11011, 2), o27 is octal 27 etc... it just converts it to the decimal value and prints it out

the only problem is when the input is for example h1B, how would i go abt assigning that B is 11?

In that program you have, try entering a 12-bit binary and see what happens.

That program reads in a decimal value. It should read in a string and then that string should be converted.

If you read in a string, then you can handle hex. If you read in a decimal value, then you cannot handle hex, because scanf's conversion of the input string to a decimal will fail.

PS
I compiled and ran that program, which I named HOSED.C:
C:> hosed
Enter an interger (0's and 1's): 111111111111
The decimal equilavent is 0.
'Nuff said?
Last edited by dwise1_aol; April 25th, 2007 at 11:04 AM.
7. hi thanks for your explanation maybe this would be clearer?
Last edited by icepricessa; April 26th, 2007 at 07:51 AM.
8. Converts a string in any base to decimal:
Code:
```unsigned int basetodec(char* value, int base)
{
int total = 0;
int digit;
char *cp = value;
char ch;

// set up an infinite loop.
// use break statement to exit it.
for ( ; ; cp++)
{
ch = toupper(*cp);
if (ch >= '0' && ch <= '9')
digit = ch - '0';
else if (ch >= 'A' && ch <= (base - 11 + 'A'))
digit = ch - 'A' + 10;
else
break;

total *= base;
total += digit;
}