码迷,mamicode.com
首页 > 其他好文 > 详细

Bit Operation (Message Compression/Decompression)

时间:2020-05-19 09:15:33      阅读:56      评论:0      收藏:0      [点我收藏+]

标签:get   opera   fclose   define   name   run   sam   fopen   ||   

  1 #include<stdio.h>
  2 #include<stdlib.h>
  3 #include<string.h>
  5 
  6 #define UNPACKED_SIZE 160
  7 #define PACKED_SIZE 120
  8 
  9 void menu();
 10 void pack();
 11 void unpack();
 12 
 13 int main()
 14 {
 15     
 16     menu();
 17     return 0;
 18 }
 19 
 20 void unpack()
 21 {
 22     char UnpackedArray[UNPACKED_SIZE];
 23     char PackedArray[PACKED_SIZE];
 24     FILE *packed_file = NULL;
 25     char Buffer[100];
 26     char file_name[100];
 27     int i; int pos; int count = 0;
 28     int bit[8]; int bit2[8]; int bit3[8];
 29 
 30     printf("Enter the file name:");
 31     fgets(Buffer,100,stdin);
 32     sscanf(Buffer,"%s",file_name);
 33         
 34 
 35     packed_file = fopen(file_name,"rb");
 36     if(packed_file == NULL)
 37     {
 38         printf("packed_file is NULL!\n");
 39         menu();
 40         exit(1);
 41     }
 42 
 43     fread(PackedArray,sizeof(char),PACKED_SIZE,packed_file);
 44     
 45     /* bit operation */
 46     for(i=0;i<UNPACKED_SIZE;i = i+4)
 47     {
 48         /* bit represents the bits of the first byte in a 4-bytes unit */    
 49         bit[0] = (PackedArray[i-count] & 0x01) == 0x01 ? 1:0;
 50         bit[1] = (PackedArray[i-count] & 0x02) == 0x02 ? 1:0;
 51         bit[2] = (PackedArray[i-count] & 0x04) == 0x04 ? 1:0;
 52         bit[3] = (PackedArray[i-count] & 0x08) == 0x08 ? 1:0;
 53         bit[4] = (PackedArray[i-count] & 0x10) == 0x10 ? 1:0;
 54         bit[5] = (PackedArray[i-count] & 0x20) == 0x20 ? 1:0;
 55         bit[6] = (PackedArray[i-count] & 0x40) == 0x40 ? 1:0;
 56         bit[7] = (PackedArray[i-count] & 0x80) == 0x80 ? 1:0;
 57         
 58         /* bit2 represents the bits of the next byte */
 59         bit2[0] = (PackedArray[i+1-count] & 0x01) == 0x01 ? 1:0;
 60         bit2[1] = (PackedArray[i+1-count] & 0x02) == 0x02 ? 1:0;
 61         bit2[2] = (PackedArray[i+1-count] & 0x04) == 0x04 ? 1:0;
 62         bit2[3] = (PackedArray[i+1-count] & 0x08) == 0x08 ? 1:0;
 63         bit2[4] = (PackedArray[i+1-count] & 0x10) == 0x10 ? 1:0;
 64         bit2[5] = (PackedArray[i+1-count] & 0x20) == 0x20 ? 1:0;
 65         bit2[6] = (PackedArray[i+1-count] & 0x40) == 0x40 ? 1:0;
 66         bit2[7] = (PackedArray[i+1-count] & 0x80) == 0x80 ? 1:0;
 67 
 68         /* bit3 works the same way as bit2*/
 69         bit3[0] = (PackedArray[i+2-count] & 0x01) == 0x01 ? 1:0;
 70         bit3[1] = (PackedArray[i+2-count] & 0x02) == 0x02 ? 1:0;
 71         bit3[2] = (PackedArray[i+2-count] & 0x04) == 0x04 ? 1:0;
 72         bit3[3] = (PackedArray[i+2-count] & 0x08) == 0x08 ? 1:0;
 73         bit3[4] = (PackedArray[i+2-count] & 0x10) == 0x10 ? 1:0;
 74         bit3[5] = (PackedArray[i+2-count] & 0x20) == 0x20 ? 1:0;
 75         bit3[6] = (PackedArray[i+2-count] & 0x40) == 0x40 ? 1:0;
 76         bit3[7] = (PackedArray[i+2-count] & 0x80) == 0x80 ? 1:0;
 77 
 78         UnpackedArray[ i ] = PackedArray[i-count];
 79         for(pos=7; pos>=6; pos--)
 80         {
 81             UnpackedArray[i] &= ~(1<<pos); 
 82         }
 83 
 84 
 85         UnpackedArray[ i+1 ] = ( PackedArray[i+1-count] << 2 );
 86         for(pos=1; pos>=0; pos--)
 87         {
 88             if( bit[ pos+6 ] )
 89             {
 90                 UnpackedArray[ i+1 ] |= 1<<pos;
 91             }
 92             else
 93             {
 94                 UnpackedArray[ i+1 ] &= ~(1<<pos); 
 95             }
 96         }
 97         for(pos=7; pos>=6; pos--)
 98         {
 99             UnpackedArray[ i+1 ] &= ~(1<<pos); 
100         }
101 
102 
103         UnpackedArray[ i+2 ] = ( PackedArray[i+2-count] << 4 );
104         for(pos=3; pos>=0; pos--)
105         {
106             if(bit2[ pos+4 ])
107             {
108                 UnpackedArray[ i+2 ] |= 1<<pos;
109             }
110             else
111             {
112                 UnpackedArray[ i+2 ] &= ~(1<<pos); 
113             }
114         }
115         for(pos=7; pos>=6; pos--)
116         {
117             UnpackedArray[ i+2 ] &= ~(1<<pos); 
118         }
119 
120         
121     
122         for(pos=5; pos>=0; pos--)
123         {
124             if(bit3[ pos+2 ])
125             {
126                 UnpackedArray[ i+3 ] |= 1<<pos;
127             }
128             else
129             {
130                 UnpackedArray[ i+3 ] &= ~(1<<pos); 
131             }
132         }
133         for(pos=7; pos>=6; pos--)
134         {
135             UnpackedArray[ i+3 ] &= ~(1<<pos); 
136         }
137 
138         /* Every time 3 bytes are unpacked to 4 bytes, count will increase 1 */ 
139         count++;
140 
141     }
142 
143     for( i=0; i<strlen(UnpackedArray); i++ )
144     {
145         UnpackedArray[i] = SMSToChar(UnpackedArray[i]);
146     }
147     
148     printf("The unpacked cstring is:%s \n",UnpackedArray);
149     fclose(packed_file);
150     menu();
151     
152 }
153 
154 void pack()
155 {
156     char UnpackedArray[UNPACKED_SIZE];
157     char PackedArray[PACKED_SIZE];
158     FILE *packed_file = NULL;
159     char Buffer[UNPACKED_SIZE+100];
160     char file_name[100];
161     int i; int pos; int count = 0;
162     int bit2[8]; int bit3[8]; int bit4[8];
163 
164     printf("Enter the file name to save the packed array:");
165     fgets(Buffer,100,stdin);
166     sscanf(Buffer,"%s",file_name);
167         
168     printf("Enter the text to pack:");
169     fgets(Buffer,UNPACKED_SIZE+100,stdin);
170     if(Buffer[0] == \n)
171     {
172         printf("Invalid input: 0 character was entered! \n");
173         menu();
174         exit(1);
175     }
176     /* Remove newline character from the input string */
177     if(strlen(Buffer) > UNPACKED_SIZE)
178     {
179         /*It will be truncated to 159 chars and NULL will be placed in the last element */
180         Buffer[UNPACKED_SIZE-1] = \0;
181     }
182     else
183     {
184         Buffer[strlen(Buffer)-1] = \0;
185     }
186 
187     sscanf(Buffer,"%[^\n]",UnpackedArray);
188     /* clean the garbage characters in UnpackedArray */
189     for(i=strlen(UnpackedArray);i<UNPACKED_SIZE;i++)
190     {
191         UnpackedArray[i] = 0;
192     }
193     
194     packed_file = fopen(file_name,"wb");
195     if(packed_file == NULL)
196     {
197         printf("packed_file is NULL!\n");
198         menu();
199         exit(1);
200     }
201     
202     for(i=0;i<strlen(UnpackedArray);i++)
203     {
204         UnpackedArray[i] = CharToSMS(UnpackedArray[i]);
205     }
206     
207     /* bit operation */
208     for(i=0;i<UNPACKED_SIZE;i = i+4)
209     {    
210         /* bit2 represents the bits of the second byte in every 4 bytes in the text*/
211         bit2[0] = (UnpackedArray[i+1] & 0x01) == 0x01 ? 1:0;
212         bit2[1] = (UnpackedArray[i+1] & 0x02) == 0x02 ? 1:0;
213         bit2[2] = (UnpackedArray[i+1] & 0x04) == 0x04 ? 1:0;
214         bit2[3] = (UnpackedArray[i+1] & 0x08) == 0x08 ? 1:0;
215         bit2[4] = (UnpackedArray[i+1] & 0x10) == 0x10 ? 1:0;
216         bit2[5] = (UnpackedArray[i+1] & 0x20) == 0x20 ? 1:0;
217         bit2[6] = (UnpackedArray[i+1] & 0x40) == 0x40 ? 1:0;
218         bit2[7] = (UnpackedArray[i+1] & 0x80) == 0x80 ? 1:0;
219 
220         /* bit3 works the same way as bit2*/
221         bit3[0] = (UnpackedArray[i+2] & 0x01) == 0x01 ? 1:0;
222         bit3[1] = (UnpackedArray[i+2] & 0x02) == 0x02 ? 1:0;
223         bit3[2] = (UnpackedArray[i+2] & 0x04) == 0x04 ? 1:0;
224         bit3[3] = (UnpackedArray[i+2] & 0x08) == 0x08 ? 1:0;
225         bit3[4] = (UnpackedArray[i+2] & 0x10) == 0x10 ? 1:0;
226         bit3[5] = (UnpackedArray[i+2] & 0x20) == 0x20 ? 1:0;
227         bit3[6] = (UnpackedArray[i+2] & 0x40) == 0x40 ? 1:0;
228         bit3[7] = (UnpackedArray[i+2] & 0x80) == 0x80 ? 1:0;
229 
230         /* bit4 works the same way as bit3 & bit2*/
231         bit4[0] = (UnpackedArray[i+3] & 0x01) == 0x01 ? 1:0;
232         bit4[1] = (UnpackedArray[i+3] & 0x02) == 0x02 ? 1:0;
233         bit4[2] = (UnpackedArray[i+3] & 0x04) == 0x04 ? 1:0;
234         bit4[3] = (UnpackedArray[i+3] & 0x08) == 0x08 ? 1:0;
235         bit4[4] = (UnpackedArray[i+3] & 0x10) == 0x10 ? 1:0;
236         bit4[5] = (UnpackedArray[i+3] & 0x20) == 0x20 ? 1:0;
237         bit4[6] = (UnpackedArray[i+3] & 0x40) == 0x40 ? 1:0;
238         bit4[7] = (UnpackedArray[i+3] & 0x80) == 0x80 ? 1:0;
239 
240         PackedArray[ i-count ] = UnpackedArray[i];
241         for(pos=7; pos>=6; pos--)
242         {             
243             if( bit2[ pos-6 ] )
244             {
245                 PackedArray[ i-count ] |= 1<<pos;
246             }
247             else
248             {
249                 PackedArray[ i-count ] &= ~(1<<pos); 
250             }
251         }
252 
253         PackedArray[ i+1-count ] = ( UnpackedArray[i+1] >> 2 );
254         for(pos=7; pos>=4; pos--)
255         {             
256             if( bit3[ pos-4 ] )
257             {
258                 PackedArray[ i+1-count ] |= 1<<pos;
259             }
260             else
261             {
262                 PackedArray[ i+1-count ] &= ~(1<<pos); 
263             }
264         }
265 
266         PackedArray[ i+2-count ] = ( UnpackedArray[i+2] >> 4 );
267         for(pos=7; pos>=2; pos--)
268         {             
269             if( bit4[ pos-2 ] )
270             {
271                 PackedArray[ i+2-count ] |= 1<<pos;
272             }
273             else
274             {
275                 PackedArray[ i+2-count ] &= ~(1<<pos); 
276             }
277         }
278 
279         /* Every time 4 bytes are packed to 3 bytes, count will increase 1 */ 
280         count++;
281 
282     }
283 
284     for(i=strlen(PackedArray);i<PACKED_SIZE;i++)
285     {
286         PackedArray[i] = 0;    
287     }
288 
289 
290     for(i=0;i<PACKED_SIZE;i++)
291     {
292         fwrite(&PackedArray[i],sizeof(char),1,packed_file);
293     }
294     fclose(packed_file);
295 
296     menu();    
297 }
298 
299 void menu()
300 {
301     char Buffer[1024];
302     char choice ;
303 
304     printf("Enter P or p: Pack and save a line of text \n");
305     printf("Enter U or u: Unpack and print a line of text  \n");
306     printf("Enter Q or q: Quit \n");
307     printf("Enter your choice: ");
308     
309     fgets(Buffer,1024,stdin);
310     sscanf(Buffer,"%c",&choice);
311 
312     if(choice == P || choice == p)
313     {
314         pack();        
315     }
316     else if(choice == U || choice == u)
317     {
318         unpack();
319     }
320     else if(choice == Q || choice == q)
321     {
322         printf("Program Exited. \n");        
323     }
324     else
325     {
326         menu();
327     }
328     
329 
330 }

 

Bit Operation (Message Compression/Decompression)

标签:get   opera   fclose   define   name   run   sam   fopen   ||   

原文地址:https://www.cnblogs.com/JasperZhao/p/12914084.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!