Encoding and Decoding text in c -


#include <stdio.h> #include <stdlib.h> #include <string.h>  file *inputfile; file *outputfile;  int encodebinary[4] = {0x00, 0x01, 0x02, 0x03}; char encodechars[4] = {':', '@', '\n', ' '};  void encode(const char * infile, const char * outfile) {      inputfile = fopen(infile, "r");     outputfile = fopen(outfile, "w");     char linebuffer[bufsiz];      if(inputfile == null)     {         perror("error while opening file.\n");         exit(exit_failure);     }      while(fgets(linebuffer, sizeof(linebuffer), inputfile))     {         for(int = 0; linebuffer[i] != 0; i++)         {             if(linebuffer[i] == encodechars[0])             {                 fprintf(outputfile, "%d", encodebinary[0]);             }             else if(linebuffer[i] == encodechars[1])             {                 fprintf(outputfile, "%d", encodebinary[1]);             }             else if(linebuffer[i] == encodechars[2])             {                 fprintf(outputfile, "%d", encodebinary[2]);             }             else if(linebuffer[i] == encodechars[3])             {                 fprintf(outputfile, "%d", encodebinary[3]);             }         }     }      fclose(inputfile);     fclose(outputfile);  }  void decode(const char * infile, const char * outfile) {      inputfile = fopen(infile, "r");     outputfile = fopen(outfile, "w");     char linebuffer[bufsiz];      if(inputfile == null)     {         perror("error while opening file.\n");         exit(exit_failure);     }      while(fgets(linebuffer, sizeof(linebuffer), inputfile))     {         for(int = 0; linebuffer[i] != 0; i++)         {             if(linebuffer[i] == '0')             {                 fprintf(outputfile, "%c", encodechars[0]);             }             else if(linebuffer[i] == '1')             {                 fprintf(outputfile, "%c", encodechars[1]);             }             else if(linebuffer[i] == '2')             {                 fprintf(outputfile, "%c", encodechars[2]);             }             else if(linebuffer[i] == '3')             {                 fprintf(outputfile, "%c", encodechars[3]);             }         }     }      fclose(inputfile);     fclose(outputfile);  }   void commands(const char * command, const char * inputfile, const char * outputfile) {     if(strcmp(command, "encode") == 0)     {         encode(inputfile, outputfile);     }     else if(strcmp(command, "decode") == 0)     {         decode(inputfile, outputfile);     } }  void testvalues(int argc, const char * argv[]) {     if(argc == 4)     {         commands(argv[1], argv[2], argv[3]);     }     else         printf("usage: ./encode [input_file] [output_file]\n"); }  //main int main(int argc, const char * argv[]) {      testvalues(argc, argv);      return 0; } 

hi there. have piece of code. code supposed text file in consisting of characters : @ "newline" , "space". these characters supposed converted binary, 0, 1, 10, 11. after need way decode original characters. can't seem figure out how able read difference between numbers, if there 001, how can know talking 0, 01, , not 00, 1. read somewhere can use bitwise operations this? appreciated!

so, have change code bit. problem when store values file encoded large file encoded. how can store values in file in such way stores values hexadecimal (or binary) encoded file smaller original file?

{0, 1, 10, 11}; not binary numbers, decimal numbers, default number format in c source code. other possible number bases hex, written prefix 0x , octal, written prefix 0. there no way write binary numbers in standard c code (probably because considered hard read humans).

so have type numbers in hex:

{0x00, 0x01, 0x02, 0x03} 

the algorithm pretty straight-forward:

  • read character file.
  • search match of character among encodechars (which should declared const char []).
  • if found, replace corresponding index in "binary".
  • decoding other way around, use binary lookup table instead.
  • if performance important, consider implementing using binary search. ideal example of binary search should used (sorted data, no duplicates).

edit

what spoke of representation of numbers programmer, inside programmer's own source code. here can use decimal, hex , octal.

there representation of numbers user, suppose looking for. can fancy.

and there representation of numbers cpu. wants binary , nothing binary.

consider this: printf("%c", 0x41).

  • the programmer sees hex 41.
  • the user sees letter a.
  • the cpu sees "store number 01000001 on stack. jump subroutine."

to display random byte binary number user, like:

#include <stdint.h>  uint8_t data = 0x41;  for(uint8_t i=0; i<8; i++) {   if( (data & (1<<i)) > 0)   {     printf("1");   }   else   {     printf("0");   }  } 

Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -