How to XOR byte arrays in Pascal

1 Answer

0 votes
program XORBytes;

const
  LEN = 5;

type
  ByteArray = array[0..LEN-1] of Byte;

procedure XorBytes(const a, b: ByteArray; var result: ByteArray);
var
  i: Integer;
begin
  for i := 0 to LEN - 1 do
    result[i] := a[i] xor b[i];
end;

procedure PrintBitsetArray(const labelText: string; const arr: ByteArray);
var
  i, bit: Integer;
begin
  Write(labelText, ': ');
  for i := 0 to LEN - 1 do
  begin
    for bit := 7 downto 0 do
      Write((arr[i] shr bit) and 1);
    Write(' ');
  end;
  WriteLn;
end;

var
  a, b, c: ByteArray;
  i: Integer;
begin
  a[0] := Ord('A'); a[1] := Ord('e'); a[2] := Ord('r'); a[3] := Ord('y'); a[4] := Ord('n');
  b[0] := Ord('A'); b[1] := Ord('l'); b[2] := Ord('b'); b[3] := Ord('u'); b[4] := Ord('s');

  XorBytes(a, b, c);

  PrintBitsetArray('a', a);
  PrintBitsetArray('b', b);
  PrintBitsetArray('c', c);

  Write('c: ');
  for i := 0 to LEN - 1 do
    Write(c[i], ' ');
end.


   
(*
run:
  
a: 01000001 01100101 01110010 01111001 01101110 
b: 01000001 01101100 01100010 01110101 01110011 
c: 00000000 00001001 00010000 00001100 00011101 
c: 0 9 16 12 29
  
*)

 



answered Jul 12 by avibootz
...