449,079 Members | 891 Online
Need help? Post your question and get tips & solutions from a community of 449,079 IT Pros & Developers. It's quick & easy.

writing bits to file

 P: n/a Hello, I am working on a compression project and I want to write ASCII characters using the minimum amount of bits. Since I will be writing ASCII characters from 0-127 I only need 7 bits to represent a character. Therefore, if I write each character at a time, I will end up writing 8 bits. One method would be to somehow concatinate all the 7 bit words I am trying to write and just pad the last byte. e.g. Read the first 7-bit word, concatinate a bit from the next word and write the byte. Concat two bits from the next word and write the byte, e.g. until I'm finished. The method seems overly complicated and I was wondering if there are any bit libraries out there that I could take advantage of? Maybe you know of another way for me to achieve this. Thanks, Matt May 21 '06 #1
7 Replies

 P: n/a "Matt Kowalczyk" wrote Hello, I am working on a compression project and I want to write ASCII characters using the minimum amount of bits. Since I will be writing ASCII characters from 0-127 I only need 7 bits to represent a character. Therefore, if I write each character at a time, I will end up writing 8 bits. One method would be to somehow concatinate all the 7 bit words I am trying to write and just pad the last byte. e.g. Read the first 7-bit word, concatinate a bit from the next word and write the byte. Concat two bits from the next word and write the byte, e.g. until I'm finished. The method seems overly complicated and I was wondering if there are any bit libraries out there that I could take advantage of? Maybe you know of another way for me to achieve this. Write a function int squashASCII(unsigned char *out, char *in) to squash a string. Return the number of bytes, which will be 1/8th less than the input, -1 on error (if someone passes a non-ASCII character). It should not be at all difficult to do. -- Buy my book 12 Common Atheist Arguments (refuted) \$1.25 download or \$7.20 paper, available www.lulu.com/bgy1mm May 21 '06 #2

 P: n/a Malcolm wrote: "Matt Kowalczyk" wroteHello,I am working on a compression project and I want to write ASCII charactersusingthe minimum amount of bits. Since I will be writing ASCII characters from0-127I only need 7 bits to represent a character. Therefore, if I write eachcharacter at a time, I will end up writing 8 bits.One method would be to somehow concatinate all the 7 bit words I am tryingtowrite and just pad the last byte.e.g. Read the first 7-bit word, concatinate a bit from the next word andwritethe byte. Concat two bits from the next word and write the byte, e.g.until I'mfinished.The method seems overly complicated and I was wondering if there are anybitlibraries out there that I could take advantage of? Maybe you know ofanotherway for me to achieve this. Write a function int squashASCII(unsigned char *out, char *in) to squash a string. Return the number of bytes, which will be 1/8th less than the input, -1 on error (if someone passes a non-ASCII character). It should not be at all difficult to do. Looks like that's what I will have to do. What I really want is a bitbuffer! I was browsing around on the internet and I found one used by tooLAME (an mpeg encoder) which hopefully won't be too complicated to integrate into my project. May 21 '06 #3