有两个字符串,假设为A,B,我想将A每次取两位然后与B的前两位进行异或,然后将结果连接成一个字符串,请问该如何编写?请给出完整的代码,谢谢~还有就是如何取固定长度的字符串,比如一个字符串有25位,我想取它的前10位如何取?

解决方案 »

  1.   

    function Copy(S; Index, Count: Integer): string;
      

  2.   

    var
      a,b,c:string;
      count:integer;
      i:integer;
      temp:string;
    begin
      a:='aabbaaccdd';
      b:='aabbcc';
      b:=copy(b,1,2);
      count:=length( a ) div 2;
      i:=1;
      while count<>0 do
      begin
        temp:=copy(a,i,2);
        if temp<>b then
          c:=c+temp;
        inc(i,2);
        inc(count,-1);
      end;
      showmessage( c );end;
      

  3.   

    前10位很简单
    Copy(A,1,10);异或后基本就是乱码了,呵呵
    const B : String = '12';
    var
     A,C:String;
     Buf:array[0..1] of byte;
     I:integer;
    begin
     A:='ABCDEFGHIJKLMNOPQRSTUVWXYZ';
     C:='';
     I:=1;
     While I< Length(A) do begin
      Buf[0]:=Ord(A[I]);
      BUf[1]:=Ord(A[I+1]);
      Inc(I,2);
      C:=C+Char(Buf[0] xor Ord(B[1])) + Char(Buf[0] xor Ord(B[2]));
     end;
     showmessage(c);  //C为结果
    end;
      

  4.   

    问题一,估计楼主是想做字符串的加密。例子:function EncryptString(sString, sKey: String): String;
    var
      iKey : Integer;
      iLoop: Integer;
    begin
      SetLength(Result, Length(sString));
      iKey := 1;
      for iLoop := 1 to Length(sString) do
      begin
        Result[iLoop] := chr(ord(sString[iLoop]) xor ord(sKey[iKey]));
        inc(iKey);
        if iKey > Length(sKey) then iKey := 1;
      end;
    end;procedure TForm1.Button1Click(Sender: TObject);
    begin
      ShowMessage(EncryptString('ABCDEFGHIJKLMN', '12'));
    end;
      

  5.   

    - -有些地方不是很明白,举个例子说把,比如固定字符4B4130与6F65进行运算,该怎么写?每次取4B4130两位然后与6F65的前两位进行异或,然后将结果连接成一个字符串
      

  6.   

    上面几位朋友的代码已经很清楚了Ord(A[index]); 就是每次取一个ansi字符并转换成ascii码形式,index取值从1-->length(A)异或 就是将ascii码与一个固定数值进行 xor 运算,运算结果是个数值,使用char(值)转换成ansi字符
      

  7.   

    我看着你们真费劲。我写一个:
    var
      Map:  array['0'..'F'] of byte =
        (0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 0, 0, 0, 0, 0, 0, $A, $B, $C, $D, $E, $F);
      Map2: array[0..$F] of char =
        ('0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
        'A', 'B', 'C', 'D', 'E', 'F');//两个Map必须是全局变量procedure TForm1.Button2Click(Sender: TObject);
    var
      S1, S2, S3: string;
      I: integer;
    begin
      SetLength(S3, Length(S1));//s1 s2必须假定长度相等。
      for I := 1 to Length(S1) do
        S3[I] := Map2[Map[S1[I]] xor Map[S2[I]]];
    //结果在S3中
    end;
      

  8.   

    取固定长度字符的问题已经解决,但是,取16进制字符还是写的很模糊,要不就是很乱,请以此例写出代码,感激!
    固定字符4B4130与6F65进行运算,该怎么写?每次取4B4130两位然后与6F65的前两位进行异或,然后将结果连接成一个字符串谢谢楼上的各位兄弟!
      

  9.   

    每次取4B4130两位然后与6F65的前两位进行异或
    是这个概念
    4 xor 6
    B xor F 
    还是这个
    $4B xor $6F
    $41 xor $65
    说清楚点
      

  10.   

    固定字符4B4130与6F65进行运算,该怎么写?每次取4B4130两位然后与6F65的前两位进行异或,然后将结果连接成一个字符串
    取4B和6F异或,得到24,41与6F异或得2E,30与6F异或得5F,然后连接起来得到242E5F
      

  11.   

    终于看明白楼主想要什么了:function XorHexString(sString, sKey: String): String;
    var
      iKey : Integer;
      iLoop: Integer;
      AByte: Byte;
    begin
      Result := '';
      iKey := 1;
      for iLoop := 1 to Length(sString) div 2 do
      begin
        AByte := StrToInt('$' + copy(sString, iLoop * 2 - 1, 2));
        AByte := AByte xor StrToInt('$' + copy(sKey, iKey * 2 - 1, 2));
        Result := Result + IntToHex(AByte, 2);
        inc(iKey);
        if iKey > Length(sKey) div 2 then iKey := 1;
      end;
    end;procedure TForm1.Button1Click(Sender: TObject);
    begin
      ShowMessage(XorHexString('4B4130', '6F65'));
    end;
      

  12.   

    我看着你们真费劲。我写一个:
    var
      Map:  array['0'..'F'] of byte =
        (0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 0, 0, 0, 0, 0, 0, $A, $B, $C, $D, $E, $F);
      Map2: array[0..$F] of char =
        ('0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
        'A', 'B', 'C', 'D', 'E', 'F');//两个Map必须是全局变量procedure TForm1.Button2Click(Sender: TObject);
    var
      S1, S2, S3: string;
      I: integer;
    begin
      SetLength(S3, Length(S1));
      for I := 1 to Length(S1) do
        S3[I] := Map2[Map[S1[I]] xor Map[S2[((I-1) mod 4)+1]]];
    //结果在S3中
    end;