首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > .NET >

关于delphi字符串取值的有关问题

2012-03-07 
关于delphi字符串取值的问题!有两个字符串,假设为A,B,我想将A每次取两位然后与B的前两位进行异或,然后将结

关于delphi字符串取值的问题!
有两个字符串,假设为A,B,我想将A每次取两位然后与B的前两位进行异或,然后将结果连接成一个字符串,请问该如何编写?请给出完整的代码,谢谢~

还有就是如何取固定长度的字符串,比如一个字符串有25位,我想取它的前10位如何取?

[解决办法]


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;
[解决办法]
前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;

[解决办法]
终于看明白楼主想要什么了:

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;

[解决办法]
我看着你们真费劲。我写一个:
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;

热点排行