Re: herding ones and zeroes into bytes



On Sun, 07 Dec 2008 21:03:36 GMT, sln@xxxxxxxxxxxxxxx wrote:

On Sat, 6 Dec 2008 20:35:46 -0700, George <george@xxxxxxxxxxxxxxx> wrote:



I continue to try to implement a black word/white word encoding similar to
the treatment given in chp. 18 of _unleashed_.

#include <stdio.h>
#include <stdlib.h>

#define PATH "george.txt"
#define NUMBER 100
#define MAXFMTLEN 2000

int main(void)
{
FILE *fp;
char pattern[MAXFMTLEN];
char lbin[NUMBER];
char line[MAXFMTLEN];

if ((fp = fopen(PATH, "r")) == NULL ) {
fprintf(stderr, "can't open file\n");
exit(1);
}

sprintf(pattern, "%%*s %%%ds", NUMBER-1);



while(fgets(line, MAXFMTLEN, fp) == line){
sscanf(line, pattern , lbin);

printf("%s\n", lbin);
}



fclose(fp);
return 0;
}

// gcc -o x.exe chad6.c

output is:

C:\MinGW\source>gcc -o x.exe chad6.c

C:\MinGW\source>x
0001000000000000001
0001000000000000001
10000011001000000000000001
10000011001000000000000001
10000011001000000000000001
10000011001000000000000001
10000011001000000000000001
10000011001000000000000001
10000011001000000000000001
10000011001000000000000001
100000001111100
100000001111100
100000001111100
1000001110110111100000000000001
1000001110110111100000000000001
1000001110110111100000000000001
1000001110110111100000000000001
1000001110110111100000000000001
1000001110110111100000000000001
0001000000000000001

The next step is to herd these into bytes. I tried to follow what Jack
Klein does, but his encode.c is too complex for me to follow, and it's 20
K. I'm able to do it in fortran and know that the file I want looks like:

C:\MinGW\source>od -tx1 -Ax -v bin3.dat

C:\MinGW\source>dump bin3.dat

000000 0d 0a 10 00 22 00 06 0c 80 01 83 20 00 60 c8 00
000010 18 32 00 06 0c 80 01 83 20 00 60 c8 00 18 32 00
000020 06 03 e4 20 c8 0f 90 76 f0 00 60 ed e0 00 c1 db
000030 c0 01 83 b7 80 03 20 6f 00 06 0e de 00 08 80 01
000040

, without the initial crlf. I added that because encode.c to try to be
kosher with the usage (it would make my code garbage). Maybe, hints of of
why I don't succeed follow:

C:\MinGW\source>gcc encode1.c -o prog.exe

C:\MinGW\source>prog
usage: encode binary-input-file, t4-output-file

C:\MinGW\source>prog bin3.dat out.t4
encoded 0 rows from bin3.dat to out.t4


It's kind of a rambling post; let me restate my intent. I'd like to herd
the ones and zeroes in char lbin[NUMBER] into bytes. I have 8 bit bytes,
but there isn't any reason not to write it portably. The final byte is to
be padded with zeroes to the left. The output I believe to be correct is
the last 62 values in the hex dump.

Thanks for your comment.

Hey, just a reminder this is not a C group, although this is easy to
do in Perl. I have no idea what your trying to accomplish but
here is a free extension course:

char *Bits =
"0001000000000000001\
0001000000000000001\
10000011001000000000000001\
10000011001000000000000001\
10000011001000000000000001\
10000011001000000000000001\
10000011001000000000000001\
10000011001000000000000001\
10000011001000000000000001\
10000011001000000000000001\
100000001111100\
100000001111100\
100000001111100\
1000001110110111100000000000001\
1000001110110111100000000000001\
1000001110110111100000000000001\
1000001110110111100000000000001\
1000001110110111100000000000001\
1000001110110111100000000000001\
0001000000000000001";

printf ("\nlength Bits = %d\nBits = %s", strlen(Bits), Bits);

if (0)
for (int i=0; i<strlen(Bits); i+=8)
{
char sbyte[9] = "00000000";
strncpy(sbyte, &Bits[i], 8);
int byte = 0;
for (int d=7; d>=0; d--)
byte += ((sbyte[d]-'0')<<(7-d));
printf ("%02x %s\n", byte, sbyte);
}
// or simply:
for (int i=0; i<strlen(Bits); i+=8)
{
int byte = 0;
for (int d=7; d>=0; d--)
byte += ((Bits[i+d]-'0')<<(7-d));
printf ("%02x\n", byte);
}

sln


C:\MinGW\source> perl sln1.pl
Operator or semicolon missing before *Bits at sln1.pl line 1.
Ambiguous use of * resolved as operator * at sln1.pl line 1.
Bareword found where operator expected at sln1.pl line 32, near ")
byte"
(Missing operator before byte?)
Bareword found where operator expected at sln1.pl line 40, near ")
byte"
(Missing operator before byte?)
Can't modify multiplication (*) in scalar assignment at sln1.pl line 21,
near "0
001000000000000001";"
(Might be a runaway multi-line "" string starting on line 2)
syntax error at sln1.pl line 26, near ")
for "
syntax error at sln1.pl line 27, near "8)
"
syntax error at sln1.pl line 29, near "&Bits["
syntax error at sln1.pl line 32, near ")
byte "
syntax error at sln1.pl line 40, near ")
byte "
Execution of sln1.pl aborted due to compilation errors.

C:\MinGW\source>
--
George

The United States of America will never be intimidated by thugs and
assassins. The killers will fail, and the Iraqi people will live in
freedom.
George W. Bush

Picture of the Day http://apod.nasa.gov/apod/
.



Relevant Pages

  • PostgreSQL 7.1.3 installation problem on FC3
    ... checking for int timezone... ... confdefs.h:7: error: syntax error before numeric constant ... /* Override any gcc2 internal prototype to avoid an error. ... /* We use char because int might match the return type of a gcc2 ...
    (Fedora)
  • Re: Duplicate Header Declaration
    ... void swap(char *s, char *t); ... int is_space; ... syntax error before '{' token ...
    (comp.lang.c)
  • compiling openoffice 1.1.3 error
    ... configure: failed program was: ... /* We use char because int might match the return type of a gcc2 ... syntax error before "udev_t" ...
    (freebsd-questions)
  • Re: Returning a function pointer
    ... returns a pointer to a function that takes an int and returns a string. ... Are you getting a syntax error? ... char *foo ...
    (comp.lang.c)
  • Re: [MFC REQUEST] Filename completion in sh(1)
    ... * This code is derived from software contributed to The NetBSD Foundation ... +static const char * ... +static int ... void ckfree; ...
    (freebsd-current)