var
  i: Integer;
  Str, Str1, Str2: String;
begin
Str1 := '00100101';
Str2 := 'A42B1234';for i := 1 to 8 do
  Str[i] := Chr(Ord(Str1 [i]) XOR Ord(Str2 [i]));.....end;这种方法我是网上找,但是发现不行,F8跟踪,Str[i] := Chr(Ord(Str1 [i]) XOR Ord(Str2 [i]));执行一次直接就跳到下面的语句.想请教怎么样可以把两个等长8位的字符串异或后成一个新的8位字符串?

解决方案 »

  1.   

    Str2 := 'A42B1234';
    要变成2进制,才能异或
      

  2.   

    for i := 1 to 8 do
    begin
      if (Str1[i]>='0') and (Str1[i]<='9') then
        a:=Ord(Str1[i])-ord('0')
      else if (Str1[i]>='a') and (Str1[i]<='f') then
        a:=Ord(Str1[i])-ord('a')+10
      else if (Str1[i]>='A') and (Str1[i]<='F') then
        a:=Ord(Str1[i])-ord('A')+10;
      if (Str2[i]>='0') and (Str2[i]<='9') then
        b:=Ord(Str2[i])-ord('0')
      else if (Str2[i]>='a') and (Str2[i]<='f') then
        b:=Ord(Str2[i])-ord('a')+10
      else if (Str2[i]>='A') and (Str2[i]<='F') then
        b:=Ord(Str2[i])-ord('A')+10;  a:=a xor b;
      if a>9 then
        Str[i] := Chr(Ord('0')+a)
      else
        Str[i] := Chr(Ord('A')+a-10);
    end;
      

  3.   

      SetLength(str, 8);  你缺少这句话
      

  4.   

    或者定义var str : string[8];
      

  5.   

    to #2楼的朋友,
    我把你的代码写成一个函数:
    function CaleValue(Str1: String; Str2: String): String;
    var
      Str: String;
      i: Integer;
      a, b: ShortInt;
    begin
      for i := 1 to 8 do
      begin
        if (Str1[i]>='0') and (Str1[i]<='9') then
          a:=Ord(Str1[i])-ord('0')
        else if (Str1[i]>='a') and (Str1[i]<='f') then
          a:=Ord(Str1[i])-ord('a')+10
        else if (Str1[i]>='A') and (Str1[i]<='F') then
          a:=Ord(Str1[i])-ord('A')+10;    if (Str2[i]>='0') and (Str2[i]<='9') then
          b:=Ord(Str2[i])-ord('0')
        else if (Str2[i]>='a') and (Str2[i]<='f') then
          b:=Ord(Str2[i])-ord('a')+10
        else if (Str2[i]>='A') and (Str2[i]<='F') then
          b:=Ord(Str2[i])-ord('A')+10;    a:=a xor b;
        if a>9 then
          Str[i] := Chr(Ord('0')+a)
        else
          Str[i] := Chr(Ord('A')+a-10);
      end;  Result := Str;
    end;
    但是执行到Str[i] := Chr(Ord('0')+a)这句就异常.
    to #3楼的朋友:我后来加了,但是还是不行.
      

  6.   

    function strxorstr(str1,str2:string):string;
    var
      i,a,b:integer;
    begin
      result:='';
      for i:=1 to 4 do
      begin
        a:=strtoint('$'+copy(str1,2*i-1,2));
        b:=strtoint('$'+copy(str2,2*i-1,2));
        result:=result+inttohex(a xor b,2);
      end;
    end;
      

  7.   

    问题解决了,我用了SuperTitan002的方法,结果是对的.谢谢各位的回帖,谢谢!