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 ...

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.

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.

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.

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.