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. #1
    Registered User kgehrma's Avatar
    Join Date
    Feb 2011
    Posts
    18

    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. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,279
    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. #3
    Registered User kgehrma's Avatar
    Join Date
    Feb 2011
    Posts
    18
    i understand what you are saying i am just a little lost on how to get my ideas down on code.

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,279
    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. #5
    Registered User kgehrma's Avatar
    Join Date
    Feb 2011
    Posts
    18
    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. #6
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,279
    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. #7
    Registered User kgehrma's Avatar
    Join Date
    Feb 2011
    Posts
    18
    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);
    }
    Last edited by kgehrma; 02-08-2011 at 08:05 AM.

  8. #8
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,831
    I think b goes up too rapidly. I thought you wanted base 3. You want b to be 3 to-the-exponent (len-k)
    Last edited by nonoob; 02-08-2011 at 08:43 AM.

  9. #9
    Registered User kgehrma's Avatar
    Join Date
    Feb 2011
    Posts
    18
    Quote Originally Posted by nonoob View Post
    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. #10
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,831
    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. #11
    Registered User kgehrma's Avatar
    Join Date
    Feb 2011
    Posts
    18
    Quote Originally Posted by nonoob View Post
    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. #12
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,831
    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. #13
    Registered User kgehrma's Avatar
    Join Date
    Feb 2011
    Posts
    18
    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. #14
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,279
    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. #15
    Registered User kgehrma's Avatar
    Join Date
    Feb 2011
    Posts
    18
    Quote Originally Posted by anduril462 View Post
    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 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. converting two keypad inputs into one decimal number
    By volkvanmyn25 in forum C Programming
    Replies: 1
    Last Post: 09-16-2010, 09:09 AM
  2. Converting 32 bit binary IP to decimal IP (vice-versa)
    By Mankthetank19 in forum C Programming
    Replies: 15
    Last Post: 12-28-2009, 06:17 PM
  3. Converting decimal to binary within a I/O program
    By RandomX in forum C Programming
    Replies: 4
    Last Post: 11-26-2006, 08:25 AM
  4. Converting decimal to hex
    By cobrakidd in forum C++ Programming
    Replies: 9
    Last Post: 02-06-2003, 10:37 AM
  5. Converting decimal to binary?
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 04-17-2002, 08:21 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21