-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile_encoding_decoding_tool.cpp
142 lines (139 loc) · 2.55 KB
/
file_encoding_decoding_tool.cpp
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
void getting_the_code_ready(int *);
void coding(char*,FILE*,int);
int decoding(char*,FILE*,int);
int checking_if_valide(char*);
int tab[255];
int tab1[255];
char thing[100000];
char filename[100];
int i=0,x,j;
int main()
{
//E:\\WindowsImageBackup\\first_file.txt
//01101000 01100101 01101100 01101100 01101111 00100000 01110100 01101000 01100101 01110010 01100101 00001010
getting_the_code_ready(tab);
char k;
FILE *pt;
printf("******************Hello there this program can help you code and decode a file using table ASCII********************* \n");
printf("enter the file path:\n");
scanf("%s",&filename);
pt=fopen(filename,"r");
if(pt==NULL)
{
printf("There has been an error");
return(1);
}
printf("Do you want to 1-code or 2-decode?\n");
scanf("%d",&x);
while(!feof(pt))
{
fscanf(pt,"%c",&thing[i]);
i++;
}
fclose(pt);
if(x==2)
{
if(1==checking_if_valide(thing))
printf("Your file has been decoded\n");
else
{
printf("\nYour file isn\'t compatible with this methde of decoding !!!\n make sure you\'ve a space between each 8 numbers\n");
printf("do you still want to decode it ?\n");
scanf("%s",&k);
if(k=='n'||k=='N')
return(0);
else
getchar();
}
}
pt=fopen(filename,"w");
if(x==1)
coding(thing,pt,k);
else if(x==2)
decoding(thing,pt,k);
fclose(pt);
}
void getting_the_code_ready(int *tab)
{
int y,x,s,t,i,r,f=0;
for(y=0;y<256;y++)
{
s=0;
x=y;
t=8;
for(i=128;i>=1,x>0;i=(i/2))
{
if(x-i<0)
r=0;
else
{
r=1;
s=s+r*pow(10,t-1);
x=x-i;
}
t=t-1;
}
tab[f]=s;//affecte les valuer dans un tablue
f++;
}
}
void coding(char *thing,FILE *pt,int k)
{
int i;
for(i=0;i<strlen(thing);i++)
{
fprintf(pt,"%08d ",tab[(int)thing[i]]);
}
}
int checking_if_valide(char*thing)
{
int i;
for(i=0;i<strlen(thing);i++)
{
if((i+1)%9==0)
{
i++;
}
if(thing[i]==' ')
return (0);
}
return(1);
}
int decoding (char*thing,FILE*pt,int k)
{
int i,j,x=0,t,b,c=0;
int f;
for(f=0;;f+=9)
{
x=0;
t=7;
for(i=0;i<8;i++)
{
b=thing[f+i]-'0';
x=x+b*pow(10,t);
t--;
if (f>strlen(thing))
{
printf("decoding complete");
return(0);
}
}
for(j=0;j<255;j++)
{
if(x==tab[j])
{
c=1;
fprintf(pt,"%c",j);
}
}
}
if(c==0)
{
printf("But, are you sure that this is a binary code ?\n");
return(0);
}
}