# Thread: Converting decimal numbers to binary example

1. ## Converting decimal numbers to binary example

Hello CL, working on a program that converts number system from binary all the way to octal. Had a few people ask me recently how would I go about doing dec -> binary. So here is a code snippet that shows exactly what I did.

Just a reminder that all this code is setup to run at bigO(0), which is the fastest runtime you could possibly get. This code is also setup to run smoothly with my much larger program of twelve different conversions.
Code:
```//fnc to convert decimal to binary
void decToBinary() {
int temp[1024];
string converted[1024];

system("cls");
cout << "Input decimal value: ";
cin >> temp[0];
cout << endl;

//convert and store in the string converted
for (int i=0; i<1023; i++) {
temp[i+1]=temp[i]/2;

if (temp[i]%2==0) {
converted[i]="0";
}
else {
converted[i]="1";
}
}

//start from the end and remove 0's
for (int j=1023; j>0; j--) {
if (converted[j]=="0") {
converted[j]="";
}
if (converted[j]=="1") {
break;
}
}

cout << "resultant: ";

//print the binary starting from the end, so you can read it from left to right instead of right to left
for (int j=1023; j>=0; j--) {
cout << converted[j];
}

cout << endl;
cout << endl;

system("pause");
system("cls");
}```

0

2. Originally Posted by Emu
Hello CL, working on a program that converts number system from binary all the way to octal. Had a few people ask me recently how would I go about doing dec -> binary. So here is a code snippet that shows exactly what I did.

Just a reminder that all this code is setup to run at bigO(0), which is the fastest runtime you could possibly get. This code is also setup to run smoothly with my much larger program of twelve different conversions.
Code:
```//fnc to convert decimal to binary
void decToBinary() {
int temp[1024];
string converted[1024];

system("cls");
cout << "Input decimal value: ";
cin >> temp[0];
cout << endl;

//convert and store in the string converted
for (int i=0; i<1023; i++) {
temp[i+1]=temp[i]/2;

if (temp[i]%2==0) {
converted[i]="0";
}
else {
converted[i]="1";
}
}

//start from the end and remove 0's
for (int j=1023; j>0; j--) {
if (converted[j]=="0") {
converted[j]="";
}
if (converted[j]=="1") {
break;
}
}

cout << "resultant: ";

//print the binary starting from the end, so you can read it from left to right instead of right to left
for (int j=1023; j>=0; j--) {
cout << converted[j];
}

cout << endl;
cout << endl;

system("pause");
system("cls");
}```
Not bad. Keep up the good work, you might inspire me to make a tut on binary.

<PvtRyan used Tapatalk to make this Post>

0

3. Nice dude! I may make the same in Java, just as a practice thing.

0

4. No problem guys :P. I also want to suggest that you both do it, because it involves complex thinking because you have to think like the computer, and know how to convert yourself before you actually can tell the computer to do it for you. I only have all the decimal conversions completed so far, meaning I have 9 more to tackle.

0

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•
All times are GMT -10. The time now is 04:23 PM.