/* Coded by Karthik Ananthapadmanaban 6:04 PM 4/11/2008 Use this function "createBinaryString" to convert a decimal number to binary The function takes 3 arguments @num -> Number to be converted to binary @ size -> Intended format - e.g. if 5 you get the output as "00000" @copyto -> Base address where the binary string is to be copies, should be of length = size The function createBinaryString uses another function toChar that converts a number to a character @i -> Number to be converted to a character */ #include #include using namespace std; char toChar(int i) { char temp[2]; sprintf(temp,"%d",i); return temp[0]; } void createBinaryString(int num, int size, char *copyto) { int i,no,cr; i=size-1; no=num; cr=size; char temp[cr]; memset(temp,'\0',cr+1); memset(temp,'0',cr+1); if (no==0) { } else { while (no!=0) { temp[i]=toChar(no % 2); no = no / 2; i--; } } strncpy(copyto,temp,size); } //Sample usage int main() { char strbin[5]; int i=0; for (i=0; i