# Thread: Simple decimal input to binary output, please advise

1. ## 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!

2. 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. 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. You're using pow where a bit-shift can be used. That's an epic fail in terms of efficiency I'm afraid.

5. Originally Posted by iMalc
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. Originally Posted by jimblumberg
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. 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