问个问题,请问为啥gsencode编码后用gsdecode解码,为啥前4个是????,测试过了,编码没有问题,解码有问题,里面的st变量前4个字符也是????,请问究竟哪里出了问题,请大佬指点迷津,谢谢!
function cutoutstring(s:PAnsichar;l,r:longint):PAnsiChar;
begin
cutoutstring:=PAnsichar(copy(s,l,r-l+1));
end;
function search(s:PAnsiChar;l,r,a:longint):longint;
var mid:longint;
begin
if(l>=r) then
begin
if(StrComp(cutoutstring(alphabet1,l*2-1,l*2),s)=0) and (a=1) then search:=l-1
else if(StrComp(cutoutstring(alphabet2,l*2-1,l*2),s)=0) and (a=2) then search:=l-1
else if(StrComp(cutoutstring(alphabet3,l*2-1,l*2),s)=0) and (a=3) then search:=l-1
else search:=0;
end
else
begin
mid:=(l+r) div 2;
search:=search(s,l,mid,a)+search(s,mid+1,r,a);
end;
end;
function cansearch(s:PAnsiChar;l,r,a:longint):boolean;
var mid:longint;
begin
if(l>=r) then
begin
if(StrComp(cutoutstring(alphabet1,l*2-1,l*2),s)=0) and (a=1) then cansearch:=true
else if(StrComp(cutoutstring(alphabet2,l*2-1,l*2),s)=0) and (a=2) then cansearch:=true
else if(StrComp(cutoutstring(alphabet3,l*2-1,l*2),s)=0) and (a=3) then cansearch:=true
else cansearch:=false;
end
else
begin
mid:=(l+r) div 2;
cansearch:=cansearch(s,l,mid,a) or cansearch(s,mid+1,r,a);
end;
end;
function findpos(str1,str2:PAnsiChar;a:longint):longint;
var len1,len2,i:longint;
s1,s2:ansistring;
bool:boolean;
begin
len1:=length(str1); len2:=length(str2);
for i:=a to len1-len2+1 do
begin
if(StrComp(cutoutstring(str1,i,i+length(str2)-1),str2)=0) then
begin
findpos:=i;
break;
end
end;
if(i>len1-len2+1) then findpos:=0;
end;
function gsencode(s:PAnsichar;a:longint):PAnsichar;
var i,d,len:longint;
st:ansistring;
begin
len:=length(s); st:='';
for i := 1 to len do
begin
d:=ord(s[i])-ord(' ')+1;
if(a=1) then st:=st+cutoutstring(alphabet1,d*2-1,d*2)
else if(a=2) then st:=st+cutoutstring(alphabet2,d*2-1,d*2)
else if(a=3) then st:=st+cutoutstring(alphabet3,d*2-1,d*2);
end;
gsencode:=PAnsiChar(st);
end;
function gsdecode(s:PAnsichar;a:longint):PAnsichar;
var i,d,len:longint;
st:ansistring;
begin
len:=length(s) div 2; st:='';
for i:=1 to len do
begin
if(cansearch(cutoutstring(s,i*2-1,i*2),1,95,a)=true) then
d:=search(cutoutstring(s,i*2-1,i*2),1,95,a)+32
else begin
randomize;
d:=random(95)+32;
end;
st:=st+chr(d);
end;
gsdecode:=PAnsiChar(st);
end;
procedure TForm1.Button1Click(Sender: TObject);
var s:PAnsiChar;
s2:Ansistring;
begin
s:=gsdecode(gsencode('ASSDWEERE23232',2),2);
ShowMessage(s);
end;
end.
function cutoutstring(s:PAnsichar;l,r:longint):PAnsiChar;
begin
cutoutstring:=PAnsichar(copy(s,l,r-l+1));
end;
function search(s:PAnsiChar;l,r,a:longint):longint;
var mid:longint;
begin
if(l>=r) then
begin
if(StrComp(cutoutstring(alphabet1,l*2-1,l*2),s)=0) and (a=1) then search:=l-1
else if(StrComp(cutoutstring(alphabet2,l*2-1,l*2),s)=0) and (a=2) then search:=l-1
else if(StrComp(cutoutstring(alphabet3,l*2-1,l*2),s)=0) and (a=3) then search:=l-1
else search:=0;
end
else
begin
mid:=(l+r) div 2;
search:=search(s,l,mid,a)+search(s,mid+1,r,a);
end;
end;
function cansearch(s:PAnsiChar;l,r,a:longint):boolean;
var mid:longint;
begin
if(l>=r) then
begin
if(StrComp(cutoutstring(alphabet1,l*2-1,l*2),s)=0) and (a=1) then cansearch:=true
else if(StrComp(cutoutstring(alphabet2,l*2-1,l*2),s)=0) and (a=2) then cansearch:=true
else if(StrComp(cutoutstring(alphabet3,l*2-1,l*2),s)=0) and (a=3) then cansearch:=true
else cansearch:=false;
end
else
begin
mid:=(l+r) div 2;
cansearch:=cansearch(s,l,mid,a) or cansearch(s,mid+1,r,a);
end;
end;
function findpos(str1,str2:PAnsiChar;a:longint):longint;
var len1,len2,i:longint;
s1,s2:ansistring;
bool:boolean;
begin
len1:=length(str1); len2:=length(str2);
for i:=a to len1-len2+1 do
begin
if(StrComp(cutoutstring(str1,i,i+length(str2)-1),str2)=0) then
begin
findpos:=i;
break;
end
end;
if(i>len1-len2+1) then findpos:=0;
end;
function gsencode(s:PAnsichar;a:longint):PAnsichar;
var i,d,len:longint;
st:ansistring;
begin
len:=length(s); st:='';
for i := 1 to len do
begin
d:=ord(s[i])-ord(' ')+1;
if(a=1) then st:=st+cutoutstring(alphabet1,d*2-1,d*2)
else if(a=2) then st:=st+cutoutstring(alphabet2,d*2-1,d*2)
else if(a=3) then st:=st+cutoutstring(alphabet3,d*2-1,d*2);
end;
gsencode:=PAnsiChar(st);
end;
function gsdecode(s:PAnsichar;a:longint):PAnsichar;
var i,d,len:longint;
st:ansistring;
begin
len:=length(s) div 2; st:='';
for i:=1 to len do
begin
if(cansearch(cutoutstring(s,i*2-1,i*2),1,95,a)=true) then
d:=search(cutoutstring(s,i*2-1,i*2),1,95,a)+32
else begin
randomize;
d:=random(95)+32;
end;
st:=st+chr(d);
end;
gsdecode:=PAnsiChar(st);
end;
procedure TForm1.Button1Click(Sender: TObject);
var s:PAnsiChar;
s2:Ansistring;
begin
s:=gsdecode(gsencode('ASSDWEERE23232',2),2);
ShowMessage(s);
end;
end.