-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathshiftup.c
More file actions
82 lines (67 loc) · 1.58 KB
/
shiftup.c
File metadata and controls
82 lines (67 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <errno.h>
#include <math.h>
char *binshift(char *s, int t)
{
char byte[9];
size_t len = strlen(s);
//char *binary = malloc(len + 1);
char *binary = malloc(len + 1);
char *input = s;
long c;
unsigned char d;
int k;
int index = 0;
char *start = binary;
char result[len];
//printf("%lu\n", len);
for(k = 0; k < len; k += 8) {
int remainder = 0;
int temp = 0;
int decimalnum = 0;
memcpy(byte, &input[k], 8);
byte[8] = '\0';
c = atoi(byte);
//printf("%lu\n", c);
while (c != 0)
{
remainder = c % 10;
c = c / 10;
decimalnum = decimalnum + remainder * pow(2, temp);
temp++;
}
//printf("%d\n", decimalnum);
if(decimalnum == 32){
c = decimalnum;
result[index] = c;
*binary++ = result[index++];
result[index] = '\0';
}
else{
c = decimalnum + (t % 128);
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 != 3) {
fprintf(stderr, "Usage: %s string\n", argv[0]);
exit(EXIT_FAILURE);
}
output = binshift(argv[1], atoi(argv[2]));
printf("%s\n", output);
//printf("%s\n", output + 1);
free(output);
exit(EXIT_SUCCESS);
}