0
xero43's Avatar, Join Date: Oct 2009
Light Poster
i wrote this code
Code:
#include <stdio.h>
#include <iostream>
using namespace std;

// text points to 8 bytes string containing 0 and 1 symbols

char ByteFromText(char* text, FILE * im)
{
    unsigned char result = 0;
    for (int i = 0; i < 8; i++)
    {
        if (text[i] == '1') 
        {
             result &= ( 1 << (7-i) );
	     fputc(result, im);
        }
    }
    return result;
}



int main ()
{
  FILE * pFile;
  FILE * image;
  char buf[9];
  char c;
  int j=0;
  pFile=fopen ("image.txt","r");
  image=fopen ("/home/fra1985/Scrivania/c++/image.jpc","w");
  if (pFile==NULL) perror ("Error opening file");
  else
  {
    
       

      while (c != EOF )
	{      
               for (j=0; j<9; j++) {
               c = fgetc(pFile);
               buf[j]=c;
                
                                    }
              
               ByteFromText(buf, image); 		 
   
    } 
	    }
	fclose (pFile);
        fclose (image);

  
  return 0;
}
and it creates a jpc file whose dimension are more than 660 kbyte, while the original image was only 64 byte; besides, if i open the jpc file with the hex editor, all of the byte are 0..
where do i wrong?
thanks
0
xero43's Avatar, Join Date: Oct 2009
Light Poster
edit, the new file has dimension equal to 277 kbyte, but the content is 0 anywhere
0
xpi0t0s's Avatar, Join Date: Aug 2004
Mentor
Try calling ByteFromText in a small test program that passes known strings of bits to ByteFromText to create results (on the screen) that you expect. So for example my original conv1010 function displayed 01100001 01100010 01100011, which we can confirm from an ASCII table, so we could call ByteFromText("01100001"); and expect it to display "a". If it doesn't, then we know there's a problem with it.

The other thing you can try is to read the 8-character strings from the file and display them to the screen as it's working on them (maybe just display the first 20 or so). This will show if there's a problem with any of those.
0
xero43's Avatar, Join Date: Oct 2009
Light Poster
ok, this is the corrected code
Code:
#include <stdio.h>
#include <iostream>
using namespace std;

// text points to 8 bytes string containing 0 and 1 symbols

char ByteFromText(char* text)
{
    char result = 0;
    for (int i = 0; i < 8; i++)
    {
        if (text[i] == '1') 
        {
             result &= ( 1 << (7-i) );
        }
    }
    return result;
}



int main ()
{
  FILE * pFile;
  FILE * image;
  char buf[8];
  char c;
  int j=0;
  pFile=fopen ("image.txt","r");
  image=fopen ("/home/fra1985/Scrivania/c++/image.jpc","w");
  if (pFile==NULL) perror ("Error opening file");
  else
  {
      c = fgetc(pFile);

      while (c != EOF )
      {      
          buf[j++] = c;
 
          if ( j == 8 )
          {
               fputc(ByteFromText(buf),  image);
               j = 0;
           }
           c = fgetc(pFile);
      }

      fclose (pFile);
      fclose (image);
  }
 
  return 0;
}
now it creates a file whose dimension are right, because the output file is 64 kbyte as the original image, but if i try to open the content with the hex editor, it continues to assume 0 values everywhere....
it seems that it doesn't save the value in the file.....
0
xpi0t0s's Avatar, Join Date: Aug 2004
Mentor
Did you do what I suggested above?
0
xero43's Avatar, Join Date: Oct 2009
Light Poster
now it works perfectly, replacing
result &= ( 1 << (7-i) );
with result |= ( 1 << (7-i) );

you can close the thread, thanks