Thread: stuffing the input bitwise

  1. #1
    Registered User
    Join Date
    Aug 2008
    Posts
    46

    stuffing the input bitwise

    i want to do stuffing... this works fine for only one charecter... if u find any flaws then please tell me where it is....
    Code:
    #include <stdio.h>
    #include <malloc.h>
    #include <stdlib.h>
    
    main()
    {
    	FILE *fp;
    	unsigned char *ch;
    	unsigned char *ch1;
    	int one = 0x80;
    	int counter = 0;
    	int count = 8;
    	int temp;
    	int i = 0, j = 0;
    
    	fp = fopen("./stuf", "r");
    	if((ch = (char *) malloc (sizeof(char))) == NULL) {
    		printf("allocation unsuccessful\n");
    		exit(1);
    	}
    
    	if((ch1 = (char *) malloc ( 6 * sizeof(char))) == NULL) {
    		printf("allocation not successful\n");
    		exit(1);
    	}
    
    	while(fread(ch, 1, 1, fp) ) {
    		if(*ch == '\n') {
    			break;
    		}
    		one = 0x80;
    		temp = 0;
    		count = 8;
    		i = 0;
    		while(one) {
    			temp = *ch & one;
    			if(temp) {
    				counter++;
    			}
    			else {
    				counter = 0;
    			}
    			temp >>= count - i - 1;
    			ch1[j] = ch1[j] | temp;
    			ch1[j] <<= 1;
    			one >>= 1;
    			i++;
    			
    			if(counter == 5) {
    				ch1[j] <<= 1;
    				counter = 0;
    			}
    		}
    				ch1[j] >>= 1;
    		j++;
    	}
    	printf("%s", ch1);
    }
    i have taken a file in which im giving some charecters which will be input for the above pgm...

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Surely, using a 4 (or 8) byte pointer and a call to malloc to store ONE byte is a bit crossing the river to get to water?

    Code:
    	if((ch = (char *) malloc (sizeof(char))) == NULL) {
    		printf("allocation unsuccessful\n");
    		exit(1);
    	}
    
    	if((ch1 = (char *) malloc ( 6 * sizeof(char))) == NULL) {
    		printf("allocation not successful\n");
    		exit(1);
    	}
    If you already know how many bytes you need, and only need a few, use simple variable declarations and take the address if needed. So both ch and ch1 should be declared as char ch; and char ch1[6] respectively.

    What input are you using to test this? You should try with character 63 -> '?' or 126 '~', as that has 6 bits set.

    Also printf("&#37;s", ch1) is very unlikely to work well, since the stuffed data may not be printable ascii codes any more.

    And your stuffing loop looks a bit messy - surely you don't need both count and i?

    And
    Code:
    ch1[j] >>= 1;
    at the end seems like fix for looping once too many - which will fail if your input sets the highest bit of a char -> you'll loose that bit when shifting it back down if you are not lucky.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Aug 2008
    Posts
    46
    i did ch1[j] >>= 1; because every time ch1 gets a bit it makes room for another bit next time... so at last also it makes room for one more bit. which will lead to wrong answer... so for that reason i did so...

  4. #4
    Registered User
    Join Date
    Aug 2008
    Posts
    46
    for pointer ch i have reserved only 1 byte.... and as you said for the characters like ~ etc... which has 6 consecutive 1s giving wrong answer... for rest of the inputs it gives right answer...

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by vapanchamukhi View Post
    i did ch1[j] >>= 1; because every time ch1 gets a bit it makes room for another bit next time... so at last also it makes room for one more bit. which will lead to wrong answer... so for that reason i did so...
    Yes, so don't do that then - you should only shift it when you actually have something to shift into ch1.

    for pointer ch i have reserved only 1 byte....
    And yes, that's exactly why you shouldn't be using a pointer - you are using a pointer to hold 1 byte. There is ABSOLUTELY no need to use a ponter here. Just take the address of ch when you pass it to fread().

    and as you said for the characters like ~ etc... which has 6 consecutive 1s giving wrong answer... for rest of the inputs it gives right answer...
    So, if you don't need to do anything, it works OK, but not when you have to do something, yes? So that's not working then...


    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem grabbing k/b input
    By falcon9 in forum C Programming
    Replies: 2
    Last Post: 10-28-2007, 11:47 AM
  2. continues input
    By Matty_Alan in forum C Programming
    Replies: 2
    Last Post: 06-22-2007, 10:04 PM
  3. Input statement problem
    By une in forum C Programming
    Replies: 3
    Last Post: 05-29-2007, 11:16 PM
  4. For loop problems, input please.
    By xIcyx in forum C Programming
    Replies: 2
    Last Post: 04-22-2007, 03:54 AM
  5. Simple Console Input for Beginners
    By jlou in forum C++ Programming
    Replies: 0
    Last Post: 06-21-2005, 01:50 PM