#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define STR_LEN 5
void binary_converter(char *);
int main()
{
char pstring[STR_LEN];
printf("Text a string of 5 characters/digits: ");
fgets(pstring, STR_LEN + 1, stdin);
// printf("main: %d \n", strlen(pstring));
// printf("%s \n", pstring);
// pstring[strlen(pstring) - 1] = '\0'; // should not minus by 1
binary_converter(pstring);
return EXIT_SUCCESS;
}
void binary_converter(char *p)
{
int str_len = strlen(p);
int i, j, k = 8, num;
// int *binary = (int*)malloc(8 * str_len);
int *binary[8 * str_len];
for (i = 0; i < 8 * str_len; i++)
binary[i] = 0;
for (i = 0; i < str_len; i++) {
j = k -1;
num = p[i];
while (num != 0) {
binary[j--] = num%2;
num /= 2;
}
k += 8; // should added by 8 every step
}
// printf("%d \n", strlen(p));
printf ("Binary Converter");
for (i = 0; i < 8 * str_len; i++) { // if start from i = 1, the output would be strange
printf("%d", binary[i]);
if ((i + 1) % 8 == 0) {
printf (" ");
}
}
printf("\n");
//delete[] binary;
}