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); }