Bit Operation (Message Compression/Decompression)
Posted jasperzhao
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Bit Operation (Message Compression/Decompression)相关的知识,希望对你有一定的参考价值。
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! "); 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 ",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] == ‘ ‘) 171 { 172 printf("Invalid input: 0 character was entered! "); 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] = ‘