# Converting from ternary to decimal

This is a discussion on Converting from ternary to decimal within the C Programming forums, part of the General Programming Boards category; I need a function to change a ternary into a decimal. I already made one that goes from binary to ...

1. ## Converting from ternary to decimal

I need a function to change a ternary into a decimal. I already made one that goes from binary to decimal, but i am a little stumped on the logic of the ternary function

here is my binary one
Code:
```int change_bin(char *bin)
{
int  b , k, n;
int  len, sum = 0;

len = strlen(bin) - 1;
for(k = 0; k <= len; k++)
{
b = 1;
n = (bin[k] - '0');
b = b<<(len-k);
sum = sum + n * b;
}
return(sum);
}```

2. Read up on positional notation, then apply similiar techniques as you did in your binary. Note that in your binary example << serves as multiplying by a power of two. E.g. << 1 is multiplying by 2^1, << 3 is like multiplying by 2^3.

3. i understand what you are saying i am just a little lost on how to get my ideas down on code.

4. I'm not going to write the code for you. Give it a shot, and lets see what you come up with. As a hint, it will look very similar to change_bin. Remember, the << is multiplication by powers of 2 (binary is base 2), so for base 3, you will need to adjust that line to make b a power of 3 instead.

5. I understand what your saying, if i include <math.h> can't i do the pow(x,y). Or is that not at all what you are trying to say.

Code:
```int change_bin(char *bin)
{
int  b , k, n;
int  len, sum = 0;

len = strlen(bin) - 1;
for(k = 0; k <= len; k++)
{
b = 1;
n = (bin[k] - '0');
b = b<<(len-k);
// so the above line might look something like b = pow(b,(len-k));
sum = sum + n * b;
}
return(sum);
}```

6. Well, that's one way to do it. My thought was more like starting b at 1 outside the loop, then multiplying it each time through the loop. A little more efficient.

7. soo something like this?

Code:
```int change_bin(char *bin)
{
int  b , k, n;
int  len, sum = 0;

len = strlen(bin) - 1;
b = 1;
for(k = 0; k <= len; k++)
{
n = (bin[k] - '0');
b = b<<(len-k);
sum = sum + n * b;
b++;
}
return(sum);
}```

8. I think b goes up too rapidly. I thought you wanted base 3. You want b to be 3 to-the-exponent (len-k)

9. Originally Posted by nonoob
I think b goes up too rapidly.
i am sorry, but this math is just confusing the hell out of me, i might take a cell phone picture of this piece of paper i have been brain storming on just to show haha.

i understand c, and i understand trinary but God help me if i have to combine the two.

10. It's easier if you start at the other end. The "units" digit.
for (k = len; k >=0; k--)
Then you could initialize b to 1 outside the loop.
In each iteration you do b = b * 3; b will go 1, 3, 9, 27, 81...
Get rid of the b++ thing.

11. Originally Posted by nonoob
It's easier if you start at the other end. The "units" digit.
for (k = len; k >=0; k--)
Then you could initialize b to 1 outside the loop.
In each iteration you do b = b * 3; b will go 1, 3, 9, 27, 81...
Get rid of the b++ thing.
So something like this?
Code:
```b = 1;

for(k = 0; k <= len; k++)
{

b = b * 3;
n = (bin[k] - '0');
b = b<<(len-k);
sum = sum + n * b;

}```

12. Except your loop has to go from the other end like I said. And you have to get rid of
b = b<<(len-k);

13. so this is my function:
the input: 22121002010111021
yeilds:370008669
it should be: 123336223? right?

Code:
```int tri2dec(char *bin)
{
int  b , k, n;
int  len, sum = 0;

len = strlen(bin) - 1;
b = 1;
for(k = len; k >=0; k--)
{
b = b * 3;
n = (bin[k] - '0');
sum = sum + n * b;

}
return(sum);
}```

14. The rightmost digit needs to be processed with b = 1 (it's the 1's column). The next digit to the left is the 3's column, then the 9's, etc. Move your b = b * 3 down to the bottom of the loop.

15. Originally Posted by anduril462
The rightmost digit needs to be processed with b = 1 (it's the 1's column). The next digit to the left is the 3's column, then the 9's, etc. Move your b = b * 3 down to the bottom of the loop.
dear lord... i finally got it, thanks so much

Page 1 of 2 12 Last