-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcipher.c
More file actions
executable file
·68 lines (61 loc) · 1.58 KB
/
cipher.c
File metadata and controls
executable file
·68 lines (61 loc) · 1.58 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <errno.h>
char *conbin(char *s)
{
if (s == NULL) {
// NULL might be 0 but you cannot be sure about it
return NULL;
}
// get length of string without NUL
size_t slen = strlen(s);
// we cannot do that here, why?
// if(slen == 0){ return s;}
errno = 0;
// allocate "slen" (number of characters in string without NUL)
// times the number of bits in a "char" plus one byte for the NUL
// at the end of the return value
char *binary = malloc(slen * CHAR_BIT + 1);
if(binary == NULL){
fprintf(stderr,"malloc has failed in stringToBinary(%s): %s\n",s, strerror(errno));
return NULL;
}
// finally we can put our shortcut from above here
if (slen == 0) {
*binary = '\0';
return binary;
}
char *ptr;
// keep an eye on the beginning
char *start = binary;
int i;
// loop over the input-characters
for (ptr = s; *ptr != '\0'; ptr++) {
/* perform bitwise AND for every bit of the character */
// loop over the input-character bits
for (i = CHAR_BIT - 1; i >= 0; i--, binary++) {
*binary = (*ptr & 1 << i) ? '1' : '0';
}
}
// finalize return value
*binary = '\0';
// reset pointer to beginning
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);
}
// TODO: check argv[1]
output = stringToBinary(argv[1]);
printf("%s\n", output);
//printf("%s\n", output + 1);
free(output);
exit(EXIT_SUCCESS);
}