herding ones and zeroes into bytes

G

George

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.
--
George

I will never relent in defending America - whatever it takes.
George W. Bush

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

Tim Greer

George said:
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)
{

Oops? Looks like you intended this for the C programming news group.
 
G

George

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

Misposted in c.l.p.misc. My apologies.

I do have an excuse, though. I got run over by a car while on my bike
three weeks ago, and I have physical deficits in my sprained hands and
awareness problems due to concussion.

I hate cars and their drivers here in desert SW. They think they're
qualifying for whatever prix gives out prizes for being the first at the
next red light.

Cheers,
--
George

Terrorist attacks can shake the foundations of our biggest buildings, but
they cannot touch the foundation of America. These acts shatter steel, but
they cannot dent the steel of American resolve.
George W. Bush

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

sln

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, 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
 
G

George

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, 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/
 
S

sln

[snip, C related post stuff]
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:

rahc *stiB =
"1000000000000001000\
1000000000000001000\
10000000000000010011000001\
10000000000000010011000001\
10000000000000010011000001\
10000000000000010011000001\
10000000000000010011000001\
10000000000000010011000001\
10000000000000010011000001\
10000000000000010011000001\
001111100000001\
001111100000001\
001111100000001\
1000000000000011110110111000001\
1000000000000011110110111000001\
1000000000000011110110111000001\
1000000000000011110110111000001\
1000000000000011110110111000001\
1000000000000011110110111000001\
1000000000000001000";

ftnirp ("\htgneln stiB = %d\stiBn = %s", nelrts(stiB), stiB);

fi (0)
rof (tni i=0; i<nelrts(stiB); i+=8)
{
rahc etybs[9] = "00000000";
ypcnrts(etybs, &stiB, 8);
tni etyb = 0;
rof (tni d=7; d>=0; d--)
etyb += ((etybs[d]-'0')<<(7-d));
ftnirp ("%x20 %s\n", etyb, etybs);
}
// ro ylpmis:
rof (tni i=0; i<nelrts(stiB); i+=8)
{
tni etyb = 0;
rof (tni d=7; d>=0; d--)
etyb += ((stiB[i+d]-'0')<<(7-d));
ftnirp ("%x20\n", etyb);
}

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>


For the Perl equivalent, you have to xor the free extension
course C code and shift left by 3 bits. Where do you think Perl
came from anyway?

Typical George W. Bush fan..

sln
======================================

use strict;
use warnings;

my $cpos = tell( DATA);
my $Bits = '';

## inline ...
while(<DATA>) {
chomp; $Bits.=$_;
while ($Bits =~ s/(\d{8})//) {
printf ("%02x\n", oct('0b'.$1));
}
}
print "\n";

# or simply, all at once ...
seek (DATA,$cpos,0);
$Bits = '';
while(<DATA>) { chomp; $Bits.=$_ }
while ($Bits =~ /(\d{8})/g) {
printf ("%02x\n", oct('0b'.$1));
}

__DATA__
0001000000000000001
0001000000000000001
10000011001000000000000001
10000011001000000000000001
10000011001000000000000001
10000011001000000000000001
10000011001000000000000001
10000011001000000000000001
10000011001000000000000001
10000011001000000000000001
100000001111100
100000001111100
100000001111100
1000001110110111100000000000001
1000001110110111100000000000001
1000001110110111100000000000001
1000001110110111100000000000001
1000001110110111100000000000001
1000001110110111100000000000001
0001000000000000001
 
G

George

For the Perl equivalent, you have to xor the free extension
course C code and shift left by 3 bits. Where do you think Perl
came from anyway?

You don't seem to have any bitshifts, even if the ultimate number of bits
is not 8.
Typical George W. Bush fan..

That I ask on-line evidences of curiosity, which, along with integrity and
ability, seem completely absent in the texas cocksucker.
sln
======================================

use strict;
use warnings;

my $cpos = tell( DATA);
my $Bits = '';

## inline ...
while(<DATA>) {
chomp; $Bits.=$_;
while ($Bits =~ s/(\d{8})//) {
printf ("%02x\n", oct('0b'.$1));
}
}
print "\n";

# or simply, all at once ...
seek (DATA,$cpos,0);
$Bits = '';
while(<DATA>) { chomp; $Bits.=$_ }
while ($Bits =~ /(\d{8})/g) {
printf ("%02x\n", oct('0b'.$1));
}

__DATA__
0001000000000000001
0001000000000000001
10000011001000000000000001
10000011001000000000000001
10000011001000000000000001
10000011001000000000000001
10000011001000000000000001
10000011001000000000000001
10000011001000000000000001
10000011001000000000000001
100000001111100
100000001111100
100000001111100
1000001110110111100000000000001
1000001110110111100000000000001
1000001110110111100000000000001
1000001110110111100000000000001
1000001110110111100000000000001
1000001110110111100000000000001
0001000000000000001

This script works flawlessly:


10
00
22
00
06
0c
80
01
83
20
00
....
07
6f
00
06
0e
de
00
08
80
01

, which matches the output in a syntax I know better:

C:\MinGW\source>x
Number of bytes written = 62

C:\MinGW\source>dump bin4.dat

C:\MinGW\source>od -tx1 -Ax -v bin4.dat
000000 10 00 22 00 06 0c 80 01 83 20 00 60 c8 00 18 32
000010 00 06 0c 80 01 83 20 00 60 c8 00 18 32 00 06 03
000020 e4 07 c8 0f 90 76 f0 00 60 ed e0 00 c1 db c0 01
000030 83 b7 80 03 07 6f 00 06 0e de 00 08 80 01
00003e

while(<DATA>) { chomp; $Bits.=$_ }

How does this execute?
--
George

For diplomacy to be effective, words must be credible - and no one can now
doubt the word of America.
George W. Bush

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

Jürgen Exner

George said:
For diplomacy to be effective, words must be credible - and no one can now
doubt the word of America.
George W. Bush

It is better to keep quiet and let people wonder if you are an idiot
than to open your mouth and remove any doubt.
-Unknown author
 
G

George

It is better to keep quiet and let people wonder if you are an idiot
than to open your mouth and remove any doubt.
-Unknown author

I thought that was attributed to Mark Twain.

Words are important. Who is Condie Rice talking about when she says:

1) Sham election
2) Sham power-sharing
3) This president must go.

During the Georgia conflict, she said "nations just don't invade each other
in the 21st century." My jaw dropped.
--
George

Senator Kerry has been in Washington long enough to take both sides on just
about every issue.
George W. Bush

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,962
Messages
2,570,134
Members
46,690
Latest member
MacGyver

Latest Threads

Top