-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExample.c
More file actions
31 lines (22 loc) · 1.17 KB
/
Example.c
File metadata and controls
31 lines (22 loc) · 1.17 KB
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
//
// This code receives user input and output's the converted input string back to the user within a loop
//
#include "BinaryConversion.h"
#define MAX_SIZE 128
#define CHAR_BIT_SIZE 8
void main() {
printf("Convert string to binary: ");
char UserInput[MAX_SIZE]; //
//
if (fgets(UserInput, sizeof(UserInput), stdin) != NULL) { // Get user input
size_t len = strlen(UserInput); //
if (len > 0 && UserInput[len - 1] == '\n') { //
UserInput[len - 1] = '\0'; //
} //
} //
//
int **buffer = (int **)calloc(strlen(UserInput), sizeof(char)); // Allocate memory to convert string
StringToBinary(UserInput, strlen(UserInput), buffer, CHAR_BIT_SIZE); // Convert string to binary
PrintBinaryString(buffer, strlen(UserInput), CHAR_BIT_SIZE); // Print the converted string
main(); // Loop
}