![]() |
| ||
| Decimal to Binary Conversation stored into a char array Hello, I am trying to create a program where I can work with an array of char that contain either 0 or 1 representing a decimal number that the user enters. Basically I need to convert a decimal number into binary and store it into a char array. I wrote this program but it is outputting something weird. char binary(int number) |
| ||
| Re: Decimal to Binary Conversation stored into a char array try something along the lines of itoa which should allow you to use base 2 to convert it to binary it would look like so: itoa(temp, chararray, 2) |
| ||
| Re: Decimal to Binary Conversation stored into a char array I cannot use itoa(); I already did that and it won't compile on my teacher's computer. Apparently she is using some website's judging process and it has a g++ compiler. Since itoa() is not a standard function, g++ doesn't support it. |
| ||
| Re: Decimal to Binary Conversation stored into a char array uhmmm... Why don't you just loop through all the bits in the value shifting it over, then checking it with a logic AND(mask 1). Add "1" to it if it's true, else "0". Simple? |
| ||
| Re: Decimal to Binary Conversation stored into a char array Quote:
|
| ||
| Re: Decimal to Binary Conversation stored into a char array Lets say you have the value ten, and in binary that is represented as "00001010." Mask(AND operator) all the bits before the first one and the truth is now false, place a zero to the array; shift the original value right once and mask again, this time the truth is true, add an "1"; repeat. Well the array would look like "01010000" if you did that, there's an easy fix or just reverse it. If that's to much for you, can you use C++'s bitset class container? |
| ||
| Re: Decimal to Binary Conversation stored into a char array just write your own itoa. #include <iostream> |
| All times are GMT -4. The time now is 4:58 am. |
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC