Thread: decoding binary

  1. #1
    Registered User
    Join Date
    Nov 2011

    decoding binary

    i have this piece of code which is decoding function for rice algorithm. which i pass as input is a binary file, which is encoded.
    working of encoder:
    the range of input in encoding fn is 0-255, and the encoder creates a combination of unary and binary, for example if i have 42, it is encoded as (010)011111- for the part in bracket it is 42 mod 8 written in binary, part except bracket is int(42/8) written in unary.the in bitfile the value 010011111 is written.

    now when i decode the file, i read bit by bit from the file, and the following fn is used which i didnt coded but got online:
    here k=3, which is the number of bits for remainder.

    /* decode input file */
        while ((bit = BitFileGetBit(bfpIn)) != EOF)
            if (1 == bit)
                byte++; //for ex. for 010011111, this part will read 5 1's, byte=00000101
                /* finished unary portion */
                tmp = byte << k; //tmp=00101000 ( 00000101 << 3)
                if (EOF == BitFileGetBits(bfpIn, &byte, k))
                    /* unary was actually spare bits */
                byte >>= (CHAR_BIT - k);        /* leftt justify bits, byte= 00000000 */
                byte |= tmp;// byte=00101000
                fputc(byte, fpOut);//here we write 40 instead of 42? where are the remaining 3 bits? i dont get this
                byte = 0;
    As i understand the unary part is decoded, but i dont see where the binary part is decoded. i dont understand how this piece of code works with input from the file. I have written for each of the decoding steps, the value for tmp and byte in the code i thought, but they are not right bc i am getting correct decoded values when i run the code, so i am not understanding some part of it. plz can anyone help me understand this code.
    If u want to see the whle code for encoder as well as decoder, it is attached

    Attached Files Attached Files
    • File Type: c r.c (7.0 KB, 85 views)

  2. #2
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    New Zealand
    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. Decoding
    By jugs in forum C Programming
    Replies: 2
    Last Post: 10-27-2009, 02:20 PM
  2. Decoding g++ executables
    By Opel_Corsa in forum Linux Programming
    Replies: 5
    Last Post: 02-20-2006, 03:03 AM
  3. Decoding st_mode
    By Happy_Reaper in forum C Programming
    Replies: 3
    Last Post: 02-08-2006, 05:40 PM
  4. Decoding
    By jk81 in forum C Programming
    Replies: 4
    Last Post: 11-12-2002, 11:24 PM
  5. decoding/encoding
    By simhap in forum C++ Programming
    Replies: 3
    Last Post: 11-18-2001, 10:35 AM

Tags for this Thread