'===========================================================================
' Subject: INVERT BITS IN A FILE              Date: 07-20-97 (20:12)       
'  Author: Andrew Below                       Code: QB, QBasic, PDS        
'  Origin: bel@obninsk.ru                   Packet: BINARY.ABC
'===========================================================================
'Hi!
'Here is a simple program, that inverts bits in
'specified file and writes modified bytes in another file.
'Encode algorithm:  1. Get byte;
'                   2. Extract bits from byte;
'                   3. Invert bits: 01101011 (in);
'                                   10010100 (out);
'                   4. Write modified byte.
'
'If you can do it faster, please E-mail me your variants.
'Thanx.
'
'Andrew Below, H.A.S.
'E-mail: bel@obninsk.ru, onxman@geocities.com
'   WWW: http://www.geocities.com/SiliconValley/Vista/4318
'
DECLARE FUNCTION Invert$ (Char$)
DECLARE FUNCTION GetDec& (Binary$)
DEFINT A-Z
DIM SHARED Bits(0 TO 7)
Bits(0) = 1
FOR i = 1 TO 7
 Bits(i) = (Bits(i - 1) * 2)
NEXT i
CLS
LINE INPUT "Enter input file name [INVERT.BAS]: ", FileName$
IF LTRIM$(RTRIM$(FileName$)) = "" THEN FileName$ = "INVERT.BAS"
LINE INPUT "Enter output file name [INVERT.BIT]: ", OutFile$
IF LTRIM$(RTRIM$(OutFile$)) = "" THEN OutFile$ = "INVERT.BIT"
DIM s AS STRING * 1
OPEN FileName$ FOR BINARY AS #1
 OPEN OutFile$ FOR BINARY AS #2
  Lenght& = LOF(1)
  PRINT "Total:"; Lenght&
  PRINT "Current:"; : y = CSRLIN: x = POS(0)
  FOR i& = 1 TO Lenght&
    LOCATE y, x: PRINT i&
    GET #1, i&, s$
    s$ = Invert$(s$)
    PUT #2, i&, s$
  NEXT i&
 CLOSE #2
CLOSE #1

FUNCTION GetDec& (Binary$)
Binary$ = LTRIM$(RTRIM$(Binary$))
Length = LEN(Binary$)
Decimal& = 0
FOR i = 1 TO Length
   Digit$ = MID$(Binary$, i, 1)
   IF Digit$ = "0" OR Digit$ = "1" THEN
      Decimal& = 2 * Decimal& + VAL(Digit$)
   END IF
NEXT i
GetDec& = Decimal&
END FUNCTION

FUNCTION Invert$ (Char$)
Bin$ = ""
FOR i = 0 TO 7
 Bin$ = Bin$ + LTRIM$(RTRIM$(STR$((ASC(Char$) AND Bits(i)) \ Bits(i))))
NEXT i
Invert$ = CHR$(GetDec&(Bin$))
END FUNCTION
