You want to make sure that your character is unsigned. (0-255) rather than from (-128-127). unsigned char uc = (unsigned char) c; Make sure your chars are in the range 0-255. c % 2 will tell you what the right-most bit is. c %2 is 0, if the number is even. c%2 is 1, if the number is odd. c/2 is the same as c with the right-most bit removed. c is 5 = 00000101 c/2 is 2 or 00000010 So c/2 % 2 gives the value of the second-to-the-rightmost bit. Similarly c/4 %2 gives the bit third from the right, and so forth. If you want to be slick, you can do rightshift instead of divide which is a little faster. (c>>1) %2 (Shifts c one position to the right, then mods by 2.) (c>>3) %2 (Shifts c three positions to the right (essentially dividing by 8) and then mods by 2. To write to the output file, you have to assemble a new char, and then write that char. 128*b0 + 64*b1 +...+ b7;