home *** CD-ROM | disk | FTP | other *** search
/ Carousel / CAROUSEL.cdr / mactosh / unix / unxbin.sha / 8to6.c next >
C/C++ Source or Header  |  1986-04-02  |  942b  |  53 lines

  1. /*
  2.  * Convert 8 bit data stream into 6 bit data stream.  
  3.  * The approach is very dumb, but I was in a hurry.
  4.  */
  5.  
  6. #include <stdio.h>
  7.  
  8. char tr[]="!\"#$%&'()*+,-012345689@ABCDEFGHIJKLMNPQRSTUVXYZ[`abcdefhijklmpqr";
  9.  
  10. getbit()
  11. {
  12.     static int c, mask = 0x00;
  13.     static int count;
  14.  
  15.     if ( mask == 0 ) {
  16.         mask = 0x80; c = getchar();
  17.         if ( c == EOF )
  18.             if ( count % 3 == 0 )
  19.                 return -1;
  20.             else
  21.                 c = 0;    /* make everything come out even */
  22.         count++;
  23.     }
  24.     if ( c & mask )
  25.     {
  26.         mask >>= 1; return 1;
  27.     } else {
  28.         mask >>= 1; return 0;
  29.     }
  30. }
  31.  
  32. main()
  33. {
  34.     int bits = 0; int c;
  35.     int count; int val;
  36.  
  37.     printf( "(This file must be converted with something)\n:" );
  38.     count = 1;
  39.     while ( ( val = getbit() ) != -1 )
  40.     {
  41.         c = c + c + val; bits++;
  42.         if ( bits == 6 ) {
  43.             bits = 0; putchar( tr[ c ] ); c = 0;
  44.             if ( count++ > 62 ) {
  45.                 count = 0; putchar( '\n' );
  46.             }
  47.         }
  48.     }
  49.     if ( bits != 0 )
  50.         write( 2, "no bits\n", 8 );
  51.     putchar( ':' ); putchar( '\n' );
  52. }
  53.