Thread: Simple decimal input to binary output, please advise

  1. #1
    Registered User
    Join Date
    Jun 2013
    Posts
    4

    Simple decimal input to binary output, please advise

    Hi all. Still trying to get going in c. I am learning c because I want to get back into programming microcontrollers, which I previously did in assembly. I wanted to make something fairly tight in terms of program memory and RAM to show me an output in binary form. It just kind of helps when you are troubleshooting a file register or serial interface when you can see the actual bit values (on a small LCD for a micro-controller) and compare it to a datasheet.

    Please evaluate my below code for efficiency. What could I do better? Is it fairly efficient, or is it bloated?

    Code:
    #include <stdio.h>
    #include <math.h>
    
    int main()
    {
        int i;
        int decimaltoconvert;
        int convertingarray[7];
        int convertingarray2[7];
    
        //this for statement calculated the 2^i for use in the converting for statement that follows
        //it only needs to be done once, at the top of a program, to set up for subsequent conversion routines
        for (i=0;i<8;i++) {
            convertingarray[i] = pow(2,i);
            }
        
        while(1) { //start of infinite while loop
    
        printf("\nEnter a decimal 255 or below to be output as binary.\n");
        scanf("%d",&decimaltoconvert);
    
        if (decimaltoconvert < 255) { //make sure this is less than 255
    
        //this for statement will do an integer division of the decimal / 2^i and set the indexed array = to 1 or 0
        for (i=7;i>=0;i--) {
            convertingarray2[i] = decimaltoconvert/convertingarray[i];  //do integer division
            if(convertingarray2[i] == 1) {                                //if is == 0, subtracting will be negative. 
            decimaltoconvert = decimaltoconvert - convertingarray[i];
                        }
            printf("%d",convertingarray2[i]);
            }
    
        } //close bracket of "<255" if statement
            else{  //this else statement will execute if decimaltoconvert > 255
            printf("The number is over 255.  Try again!");
                } 
        } //close bracket for infinite while loop
    
    }
    Also, how might I go about putting that into a function that I could call?
    Thanks!
    Last edited by wannaBinventor; 06-22-2013 at 11:18 AM.

  2. #2
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Code:
    convertingarray2[i] = decimaltoconvert/convertingarray[i];  //do integer division
    could be
    Code:
    convertingarray2[i] = ( decimaltoconvert >> i);  //do integer division
    you wouldn't need convertingarray at all

    Kurt

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    You have a couple of other problems, first look at this snippet:
    Code:
       printf("\nEnter a decimal 255 or below to be output as binary.\n");
        scanf("%d",&decimaltoconvert);
    
        if (decimaltoconvert < 255) { //make sure this is less than 255
    You tell the user to enter a value equal to or less than 255, yet your if statement rejects values equal to 255.

    Next look closely at this snippet:
    Code:
        int convertingarray[7];
        int convertingarray2[7];
    
    ...
        for (i=0;i<8;i++) {
            convertingarray[i] = pow(2,i);
            }
    ...
     for (i=7;i>=0;i--) {
    You are accessing your arrays out of bounds. You declared them with a size of 7 and then try to access element 7. Remember arrays start at zero and stop at size - 1.


    Jim

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    You're using pow where a bit-shift can be used. That's an epic fail in terms of efficiency I'm afraid.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  5. #5
    Registered User
    Join Date
    Jun 2013
    Posts
    4
    Quote Originally Posted by iMalc View Post
    You're using pow where a bit-shift can be used. That's an epic fail in terms of efficiency I'm afraid.
    Indeed. That's why I'm here!

  6. #6
    Registered User
    Join Date
    Jun 2013
    Posts
    4
    Quote Originally Posted by jimblumberg View Post
    You have a couple of other problems, first look at this snippet:

    You tell the user to enter a value equal to or less than 255, yet your if statement rejects values equal to 255.

    Next look closely at this snippet:
    Code:
        int convertingarray[7];
        int convertingarray2[7];
    
    ...
        for (i=0;i<8;i++) {
            convertingarray[i] = pow(2,i);
            }
    ...
     for (i=7;i>=0;i--) {
    You are accessing your arrays out of bounds. You declared them with a size of 7 and then try to access element 7. Remember arrays start at zero and stop at size - 1.


    Jim
    Thanks the reply, I've gotten rid of the first for statement with the pow() now. I guess I don't fully grasp the array size thing. Perhaps I'm mistaken, but I was thinking that declaring "convertingarray[7] created 8 elements. Element 0, element 1....to element 7. I was thinking of it in terms of how there are 8 bits in a byte, but the first bit is 0. If I've done it wrong, why is the program working correctly? I don't mean that sarcastically, I really want to learn why.

  7. #7
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    Perhaps I'm mistaken, but I was thinking that declaring "convertingarray[7] created 8 elements.
    Yep, you're mistaken. It creates an array with 7 elements, not 8.

    I was thinking of it in terms of how there are 8 bits in a byte, but the first bit is 0.
    Yep, there are 8 bits in a byte, normally labeled 0 - 7. (count them 0,1,2,3,4,5,6,7).

    If I've done it wrong, why is the program working correctly?
    It isn't working correctly for me. It crashes because of this problem. When you access your array out of bounds your program may crash, or may just overwrite some other variable, you never know, this is called undefined behavior, anything can happen.

    Jim

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. a simple input to output c copy program
    By Nishant Dwivedi in forum C Programming
    Replies: 7
    Last Post: 12-13-2012, 07:49 AM
  2. Simple output/input program not working?
    By tmac619619 in forum C Programming
    Replies: 3
    Last Post: 10-07-2012, 10:31 PM
  3. Pls advise why is the output like this?
    By jollykiddo in forum C Programming
    Replies: 4
    Last Post: 02-28-2012, 11:11 AM
  4. File Input and Output, simple.
    By Vber in forum C Programming
    Replies: 5
    Last Post: 11-17-2002, 02:57 PM
  5. decimal to binary, decimal to hexadecimal and vice versa
    By Unregistered in forum C++ Programming
    Replies: 9
    Last Post: 12-08-2001, 11:07 PM