How to compress data to a binary file?

This is a discussion on How to compress data to a binary file? within the C Programming forums, part of the General Programming Boards category; i need to compress an array of chars, so that my new data will be written to a binary file, ...

  1. #1
    Registered User
    Join Date
    Jun 2010
    Posts
    4

    How to compress data to a binary file?

    i need to compress an array of chars, so that my new data will be written to a binary file, with only the first 5 bits of each byte.

    if i have an array of
    Code:
    {1,2,3}
    i want to write to my binary file:
    00001 00010 00011 (without the spaces of course)

    can someone tell me a good way of doing that?

    thanks

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    short int foo = (0x1f & v1) | ((0x1f & v2) << 5) | ((0x1f & v3) << 10);
    fwrite( &foo, sizeof foo, 1, myfile);
    You could use a bitfield or a union or something else. You can also swap the order around above in case you don't like the way they're stuck together.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Jun 2010
    Posts
    4

    thanks

    hmm, that's nice

    how about a more generic solution where i get an array and its size, and i want to do this for the whole array?

    and can you show the other way to do this with a union or bitfield?

    thanks

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    Why specifically 5-bits? Can you be sure that every value is no greater than 31? Is the number of elements in the array a multiple of 8?
    Are you trying to fit with some existing format or are you just trying to compress your own data by any reasonable means necessary?

    I can go as far as providing the code for whatever method you actually want to use, but I need to know more background information to provide the best solution.
    Last edited by iMalc; 06-16-2010 at 02:35 AM.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  5. #5
    Registered User
    Join Date
    Jun 2010
    Posts
    4

    original problem

    thank you for the quick answer!

    the original problem is as follows:

    i have a struct:

    Code:
    typedef struct {
      char *name;
      unsigned long int id;
      int grade[2];
    } FStudent;
    i wrote this struct to a text file as follows:
    id<tab>name<tab>grade[0]<tab>grade[1]<newline>
    and i sorted the text file by name (i read it into a binary search tree and wrote it in order to a new text file) <- if u have a neater solution for sorting the file i would gladly hear about it.

    now i need to encrypt it and write it to a binary file with these instructions:
    the name should be encrypted as follows (uppercase only) each char will be converted to a number as follows A = 1, B=2 ... so if i have "ABA" it will be converted to 121 -> 00001 00010 00011 to the binary file (no spaces)

    id will be written as - is to the binary file, and grades which are bwn 0-100 will be written as 7 bits each grade

    after writing to the binary file, i need to encrypt the binary file so that each 4 bits will be changed as follows : bit 4<->1 and 3<->2
    so if i have a byte: 1001 0101 the result would be 1001 1010

    after all that, i need to be able to read the encrypted binary file back to the struct

    thanks for the great help!

  6. #6
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,415
    O_o

    This really isn't a compression or encryption method; it is more serialization to a very specific file format.

    It sounds like you have all the instructions you need to manage this task. Quzah showed you how to do some binary stuff. What does your code look like so far?

    And just for the record, the design of the file format is seriously flawed. You can write the producer. It isn't difficult. You can't write the consumer. Where did you get this format?

    Soma
    Last edited by phantomotap; 06-16-2010 at 03:35 AM. Reason: none of your business

  7. #7
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    You could do all I/O in 40 bit chunks, that are either full or padded to deal with alignment issues.

  8. #8
    Registered User
    Join Date
    Jun 2010
    Posts
    4

    School excercise...

    this is just a school excersice... i'm currently in my first year studying Computer science,

    so far i managed to do well, but this bit manipulation startled me a bit,
    i'll give it a try and get back to you guys if there are any problems

    i think i got the main idea...

    thanks again

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 09:35 AM
  2. reading binary file with different data types
    By larne in forum C Programming
    Replies: 8
    Last Post: 07-29-2008, 11:12 AM
  3. How to write image data to binary PGM file format(P5)?
    By tommy_chai in forum C Programming
    Replies: 6
    Last Post: 11-03-2007, 11:52 PM
  4. HUGE fps jump
    By DavidP in forum Game Programming
    Replies: 23
    Last Post: 07-01-2004, 11:36 AM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 05:10 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21