forked from ScaryTea/STM32F103
-
Notifications
You must be signed in to change notification settings - Fork 0
/
to01.c
52 lines (45 loc) · 1006 Bytes
/
to01.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
#include <stdio.h>
#include <stdlib.h>
void printBin(unsigned char c);
unsigned long counter = 0;
int main(int argc, char *argv[]) {
printf("argv[0] = %s, argv[1] = %s\n", argv[0], argv[1]);
FILE* f = fopen(argv[1], "r");
if(f == NULL)
{
printf("Error openning the file\n");
return 1;
}
fseek(f, 0, SEEK_END);
int fsize = ftell(f);
rewind(f);
unsigned char* input = (unsigned char*) malloc(sizeof(unsigned char)*fsize);
fread(input, sizeof(unsigned char), fsize, f);
printf("Read file: [%s]\n",input);
for(int i = 0; i < fsize; i++)
printBin(input[i]);
printf("\n");
fclose(f);
printf("Totally %lu bits\n",counter);
}
void printBin(unsigned char c) {
int a = (int)c;
int b = 2;
int i = 0;
unsigned char rests[10];
while(a >= b) {
rests[i] = (unsigned char)(a % b);
a = a / b;
i++;
}
rests[i] = (unsigned char)(a);
for(int j = 7; j > i; j--) {
printf("0");
counter++;
}
for(int j = i; j >= 0; j--) {
printf("%X",rests[j]);
counter++;
}
printf(" ");
}