-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathText_To_Binary_Converter.c
56 lines (54 loc) · 1.17 KB
/
Text_To_Binary_Converter.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// Note: Try to run this Code on DEV-C++
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void decimalToBinary(int decimal, char *octet)
{
octet = octet + 8;
*octet = '\0';
if (decimal == 0)
{
octet = octet - 8;
octet = "00000000";
}
else
{
while (decimal > 0)
{
octet--;
*octet = decimal % 2 + '0';
decimal = decimal / 2;
}
}
}
void textToBinary(char *text, int textLength, char *binary, int binaryLength)
{
char *octet = malloc(8);
while (*text)
{
decimalToBinary(*text, octet);
while (*octet)
*binary++ = *octet++;
*binary++ = ' ';
++text;
octet = octet - 8;
}
*binary = '\0';
free(octet);
}
int main()
{
system("color f0");
char text[101];
char *binary;
int textLength, binaryLength;
gets(text);
textLength = strlen(text);
binaryLength = textLength * 8;
binary = malloc(binaryLength + 1);
textToBinary(text, textLength, binary, binaryLength);
printf("\n\n\n\t\t\t\t\tBINARY CODES\n\n\t%s\n\n\n\n", binary);
free(binary);
system("pause");
return 0;
}