-1

My program stores Huffman code in a char[8] variable. I want to store it in an unsigned char variable. I do it, but don't think it works correctly because when I used the following code to extract my file it didn't work:

unsigned char bit2byte ( unsigned char bits[8] ) {
    unsigned char x = 0; 

    for ( int k = 0; k < 8; k++ ) {
        if ( bits[k] == '1' ) 
            x = x | 1;

        x <<= 1; 
    }

    return x; 
}
Mateen Ulhaq
  • 24,552
  • 19
  • 101
  • 135
zahratTZ
  • 21
  • 1
  • 4
  • 1
    What makes you think this doesn't work correctly? Where is your byte2bit function? – bdonlan Jul 04 '11 at 23:44
  • 1
    There really are so many unknowns about what you're trying to do that this question is really pointless – James Jul 04 '11 at 23:47

3 Answers3

3

What about this line:

if ( bits[k] == '1' ) 

does the bits array store your bits as ASCII characters or as digital values, i.e. what happens if you try

if ( bits[k] == 0x01 )

You'll probably downvote me for not being able to read your mind...

James
  • 9,064
  • 3
  • 31
  • 49
0

Huffman is an compression scheme, and if you want to read a Huffman encoded file you most likly want to decode it (i.e. uncompress it)

http://en.wikipedia.org/wiki/Huffman_coding

In Huffman encoded data, each character is represented as a variable number of bits, and hence you cannot process a file by simply passing in a fixed portion of a file expecting to return a single byte in each call -- you have to keep state of how many bits are consumed in each call, and where to start processing in the bit stream for the extracting the next byte.

To correctly decode Huffman data, you will need the encoding tree (see the wikipedia link) -- this tree is most likely stored within the files as well -- so really your file will most likely have two parts: (1) The encoding/decoding tree, and (2) the data -- how that is stored in the file is implementation specific, so you will need the specification for that first before you attempt to decode anything.

Hope this helps.

Soren
  • 14,402
  • 4
  • 41
  • 67
  • my code is a little different. i sort my list and fetch 2 minimum of list and add sum of them to list and remove this 2 nodes and then add them in another list. do this for all nodes. and then in list2 i read repeat of 2 first nodes then search for node that it's repeat is equal to them and set links from 2 nodes to this node. this make code correctly – zahratTZ Jul 05 '11 at 04:27
  • and this is my code : void Huffman::coding(Huffman *n,Huffman *l){ Huffman *node; struct code mys; int k=7; node=n; unsigned char x=0; while(node->repeat!=l->repeat){ mys.sCode[k]='0'+node->code; --k; node=node->pLink; } if(k<8) for(int i=k;0<=i;i--) mys.sCode[i]='0'; mys.name=n->character; mys.baseCode=bit2byte(mys.sCode); body.push_back(mys); } – zahratTZ Jul 05 '11 at 04:27
  • Im not sure I can decipher what that code will do, nor is it clear if you are trying to construct or use the Huffman tree. I assume the other answer of changing the '1' to 0x01 did not help you, so you probably need to revise the description of your problem to include how you have obtained the decoding tree, and how you are using it for decoding the content. – Soren Jul 07 '11 at 04:59
-1

I'm not clear on what you mean by "doesn't work" but it could be you need to go the other way.

for (int k = 7; k >= 0; k--) {

and everything else as before.

Of course, I also don't know why you ever use 8 bytes to store only 8 bits of information.

krasnerocalypse
  • 966
  • 6
  • 6
  • i use your solution but :( , i didn't understand what did you mean"I also don't know why you ever use 8 bytes to store only 8 bits of information." – zahratTZ Jul 04 '11 at 22:36
  • @zahrat An `unsigned char` is 8-bits. @krasnerocalypse is saying that should store the bits directly into one `unsigned char` instead of splitting it up into 8 of them. – Mateen Ulhaq Jul 04 '11 at 22:41
  • Well, if you can convert this unsigned char[8] to a single unsigned char without losing any information, why don't you use an unsigned char all the time? – krasnerocalypse Jul 04 '11 at 22:42
  • i really don't know!i write this code according my file to another file for make it zip. but when i want to extract it ,result is false. so result of bit2byte is incorrect. what should i do! – zahratTZ Jul 04 '11 at 22:48
  • Look closer. This is indeed packing into a single byte. As for bit order, that's a matter of convention. – bdonlan Jul 04 '11 at 23:44
  • I know it's packing into a single byte. What I'm saying is, why is it ever stored in any format other than a single byte? And of course bit order is "a matter of convention". That doesn't necessarily mean it's not the source of his problem. – krasnerocalypse Jul 05 '11 at 00:02
  • Presumably it's unpacked in preparation for some other manipulation. Unpacking it a bit at a time inline with usage may be more efficient, but for an exercise, it's a perfectly reasonable approach. – bdonlan Jul 05 '11 at 01:57
  • Basically, zahrat, we can't help you until we know a little bit more about what your problem is or what the rest of your code is doing. – krasnerocalypse Jul 05 '11 at 02:16