-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbincon.c
More file actions
52 lines (43 loc) · 1.02 KB
/
bincon.c
File metadata and controls
52 lines (43 loc) · 1.02 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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <errno.h>
char *bincon(char *s)
{
char byte[9];
size_t len = strlen(s);
//char *binary = malloc(len + 1);
char *binary = malloc(len + 1);
const char *input = s;
unsigned char c;
int k;
int index = 0;
char *start = binary;
char result[256];
for(k = 0; k < len; k += 8) {
memcpy(byte, &input[k], 8);
byte[8] = '\0';
c = (unsigned char)strtol(byte, 0, 2);
result[index] = c;
*binary++ = result[index++];
result[index] = '\0';
//printf("%s\n", result);
}
*binary = '\0';
binary = start;
return binary;
}
int main(int argc, char **argv)
{
char *output;
if (argc != 2) {
fprintf(stderr, "Usage: %s string\n", argv[0]);
exit(EXIT_FAILURE);
}
output = bincon(argv[1]);
printf("%s\n", output);
//printf("%s\n", output + 1);
free(output);
exit(EXIT_SUCCESS);
}