Thread: need help with file compression program

  1. #16
    Registered User
    Join Date
    Nov 2011
    Posts
    82
    thanks alot iMalc, i find your post later.. ok i understood how to work breaking these bits but how can i read these bits breaking it in 5..

    like there are 2 bytes 00000000 01000100 and now i want to read 5 bits and replace it with a,b, c respectively..
    i just knw to chck the complete byte through if (char = 'a')

    how can i chck these 5 bits for decompressing??

    like i have to work on small bits.. that if the next 5 bits == 00000 than replace with a
    .....
    Last edited by fredsilvester93; 02-05-2012 at 04:01 AM.

  2. #17
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    I would focus on writing two complementary routines. One which takes a bunch of 8-bit characters and packs them into say 5 bits, writing that out as each byte is filled, and one which does the opposite.

    To do that, have some int variable that holds the bits, and another that holds the count of how many bits it is holding.
    Example, storing 00000, 00001, and 00010:

    First call:
    data = ...0000000, bitsUsed = 5
    Since bitsUsed is less than 8, we can't write anything out yet.
    Next call:
    data = ...0000 00000001, bitsUsed = 10
    Now since bitsUsed is greater than 8, we can write out a byte to file. Using bit shifts we write out 00000000 and leave the remaining ..0001 in the buffer, decreasing bitsUsed by the amount we wrote out (8), leaving that set to 2.

    Next call:
    data = ..000100010, bitsUsed = 7
    Not quite 8 bits used, so we cant write out anything yet.
    However we've now reached the end, so we must flush out the remaining bits by writing a byte anyway. The last bit you just set to zero.

    When reading it in the process is similar. After extracting three lots of 5-bit values from the buffer, you'll be lewft with only 1 bit which is not enough for a 5-bit code, you you just ignore it.
    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"

  3. #18
    Registered User
    Join Date
    Nov 2011
    Posts
    82
    HEY!! I understood what you said and tried to make an algorithm which will work on this..... for Example
    i created an ASCII like for 0(47) my 5bit ascii is 00000 for 1(48)00001 ..... for A(65) my ascii is 01011 for B(66) my ascii is 01100 and so on..

    now i have charachter in
    Code:
    array arr[36]={0,1..........,A,B.........Z}
    and compressed[(36*5)/8]= {(00000000),(01011000),.......,(..01011011),(00......)...........}
    If i type SAQ
    then with 111010101111011.
    (11101010) 1st byte
    first 5 contains alternate for S and 3 for A
    i can extract it using Shift operator

    (1111011.) 2nd byte
    contains 2 bits for A

    my question is that how can i extract last 2 bit from 1st byte and add it at the starting of 2nd byte ?????? so that 2nd byte can become (01011....)

    help me plz!!!

  4. #19
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    The decompression is similar to the compression. When you want to read a 5-bit value out, if your bitsUsed is less than 5 then you read in a byte, shift the existing data bits left 8 bits and or in the new 9 bits on the right. Lastly, you increase the bitsUsed by 8. Then you can perform your exract of 5 bits.

    I hope that gets you there or most of the way because I'm going OTN (off the net) for a few days.
    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"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compression/Decompression Wave File and MP3
    By cindy_16051988 in forum Projects and Job Recruitment
    Replies: 51
    Last Post: 04-29-2006, 06:25 AM
  2. Huffman Compression Program
    By ChadJohnson in forum C++ Programming
    Replies: 13
    Last Post: 01-23-2005, 02:49 PM
  3. File Compression
    By Prelude in forum Tech Board
    Replies: 16
    Last Post: 01-01-2005, 06:53 PM
  4. File compression
    By dit6a9 in forum Windows Programming
    Replies: 1
    Last Post: 08-05-2004, 10:44 AM
  5. File Compression
    By Gr3g in forum C++ Programming
    Replies: 1
    Last Post: 04-16-2002, 04:59 PM