I wrote program to convert decimal to binary for practice purposes but i get some strange output. When doing modulo with decimal number, i get correct value but what goes in array is forward slash? I am using char array for being able to just use output with cout <<.
// web binary converter: http://mistupid.com/computers/binaryconv.htm
#include <iostream>
#include <math.h>
#include <malloc.h> // _msize
#include <climits>
#define WRITEL(x) cout << x << endl;
#define WRITE(x) cout << x;
using std::cout;
using std::endl;
using std::cin;
char * decimalToBinary(int decimal);
void decimal_to_binary_char_array();
static char * array_main;
char * decimalToBinary(int decimal) // tied to array_main
{
WRITEL("Number to convert: " << decimal << "\n");
char * binary_array;
int t = decimal, // for number of digits
digits = 0, // number of digits
bit_count = 0; // total digit number of binary number
static unsigned int array_size = 0;
if(decimal < 0) { t = decimal; t = -t; } // if number is negative, make it positive
while(t > 0) { t /= 10; digits++; } // determine number of digits
array_size = (digits * sizeof(int) * 3); // number of bytes to allocate to array_main
WRITEL("array_size in bytes: " << array_size);
array_main = new char[array_size];
int i = 0; // counter for number of binary digits
while(decimal > 0)
{
array_main[i] = (char) decimal % 2 + '0';
WRITE("decimal % 2 = " << char (decimal % 2 + '0') << " ");
WRITE(array_main[i] << " ");
decimal = decimal / 2;
WRITEL(decimal);
i++;
}
bit_count = i;
array_size = bit_count * sizeof(int) + 1;
binary_array = new char[bit_count * sizeof(int)];
for(int i=0; i<bit_count+1; i++)
binary_array[i] = array_main[bit_count-1-i];
//array_main[bit_count * sizeof(int)] = '\0';
//WRITEL("\nwhole binary_array: "); for(int i=0; i<array_size; i++) WRITE(binary_array[i]); WRITEL("\n");
delete [] array_main;
return binary_array;
}
int main(void)
{
int num1 = 3001;
// 3001 = 101110111001
// 300 = 100101100
// 1000 = 1111101000
// 1200 = 10010110000
// 1000000 = 11110100001001000000
// 200000 = 110000110101000000
array_main = decimalToBinary(num1);
WRITEL("\nMAIN: " << array_main);
cin.get();
delete [] array_main;
return 0;
}
The output:
Number to convert: 3001
array_size in bytes: 48
decimal % 2 = 1 / 1500
decimal % 2 = 0 0 750
decimal % 2 = 0 0 375
decimal % 2 = 1 1 187
decimal % 2 = 1 / 93
decimal % 2 = 1 1 46
decimal % 2 = 0 0 23
decimal % 2 = 1 1 11
decimal % 2 = 1 1 5
decimal % 2 = 1 1 2
decimal % 2 = 0 1 1
decimal % 2 = 1 1 0
MAIN: 1111101/100/
What are those forward slashes in output (1111101/100/)?