Like Tree1Likes
  • 1 Post By africanwizz

Binary to decimal c code

This is a discussion on Binary to decimal c code within the C Programming forums, part of the General Programming Boards category; Hello experts i need a binary to decimal code . i am interfacing an ADC toa controller to display the ...

  1. #1
    Registered User
    Join Date
    Mar 2011
    Location
    India ,Kerala
    Posts
    23

    Binary to decimal c code

    Hello experts i need a binary to decimal code .
    i am interfacing an ADC toa controller to display the the corresponding voltage .
    i googled got a lot of but i am facing some confusion please any body provide a sample with logic.
    soryy i am not good in c programming

  2. #2
    Registered User
    Join Date
    Nov 2012
    Posts
    147
    This seems like a highly unnecessary code but since you asked, to convert to decimal take the last digit and multiply by 2^0 ,the next digit by 2^1 etc until you reach the MSB then sum up the results. that's the logic. figure out a code.
    thannara123 likes this.

  3. #3
    Registered User Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    650
    Quote Originally Posted by africanwizz View Post
    This seems like a highly unnecessary code but since you asked, to convert to decimal take the last digit and multiply by 2^0 ,the next digit by 2^1 etc until you reach the MSB then sum up the results. that's the logic. figure out a code.
    I think that's the reverse of what thannara123 (probably) wants.

    To convert a binary number (n) and display it in base10 (decimal) you want to repeatedly get the remainder of n divided by 10 (this is the right-most digit) and print it, divide n by 10, and continue until n is 0. It's the same principle as africanwizz suggested, of course. The thing to look out for is that if you implement exactly as I've described the digits will be printed in reverse.

    The maximum number of digits in a (decimal) natural number is floor(log10(n)+1) so you can probably pre-allocate a destination array using malloc and that formula to to work out how much room you'll need, OR if you know the maximum that n can be you can just have an array with floor(log10(n)+1) + 1 elements in it. Store the digits in the array using the above method, terminate the array with \0 (to make it a string) and print it.

    Another alternative is sprintf() but depending on what you're doing and how much memory you have, perhaps implementing your own "binary to decimal" function is preferable.

    Edit: Yet another alternative is a recursive implementation of your "print" function.
    Last edited by Hodor; 12-26-2013 at 05:06 AM. Reason: another alt.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 11
    Last Post: 07-05-2011, 01:09 AM
  2. Binary to Decimal help...
    By anarcho in forum C Programming
    Replies: 10
    Last Post: 07-01-2010, 12:21 PM
  3. binary to decimal ! how?
    By o0o in forum C++ Programming
    Replies: 8
    Last Post: 12-23-2003, 09:31 PM
  4. decimal to binary
    By jk81 in forum C Programming
    Replies: 4
    Last Post: 09-12-2002, 03:00 PM
  5. decimal to binary, decimal to hexadecimal and vice versa
    By Unregistered in forum C++ Programming
    Replies: 9
    Last Post: 12-08-2001, 10:07 PM

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