-
Notifications
You must be signed in to change notification settings - Fork 0
/
bit.c
56 lines (49 loc) · 930 Bytes
/
bit.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
53
54
55
56
#include <stdlib.h>
#include <stdio.h>
#include "bit.h"
void dec_to_bin(int num, char* bin){
int aux;
for (aux = 7; aux >= 0; aux--)
{
if (num % 2 == 0)
bin[aux] = '0';
else
bin[aux] = '1';
num = num / 2;
}
}
char pega_1(FILE* file, char* buf, int* pos){
char c;
c = buf[*pos];
*pos = *pos + 1;
if (*pos>7){
unsigned char a = 0;
fread(&a,1,1,file);
dec_to_bin(a,buf);
*pos = 0;
}
return c;
}
unsigned char pega_8(FILE* file, char* buf, int* pos){
char aux[8];
for (int i = 0;i<8;i++){
aux[i]=pega_1(file,buf,pos);
}
return binstr_to_ascii(aux);
}
double elev(int num, int exp){
double resp=1;
for(int i=0;i<exp;i++){
resp*=num;
}
return resp;
}
unsigned char binstr_to_ascii(char* buf){
int s = 8;
unsigned char dec = 0;
int i = 0;
while (s--){
dec = dec + elev(2,i++)*(buf[s]-'0');
}
return dec;
}