C++ 代码转 Delphi,会者不难,请帮帮忙,谢谢(50分)

  • 主题发起人 主题发起人 9903
  • 开始时间 开始时间
9

9903

Unregistered / Unconfirmed
GUEST, unregistred user!
// Returns a char that has been converted to uppercase.
char ucase(char c)
{
if(c>='a' &&amp
c<='z')
c+='A'-'a';
return c;
}
////////////////////////////////////////////////
ansiuppercase(c:char)

// hex2int
int hex2int(char c)
{
if(c>='A' &amp;&amp
c<='F')
return(int)(c-'A'+10);
else if(c>='a' &amp;&amp
c<='f')
return(int)(c-'a'+10);
else if(c>='0' &amp;&amp
c<='9')
return(int)(c-'0');
return 0;
}
/////////////////////////
function hex2int(c:string):integer;
begin
result := strtoint('$' + c)
end;


// int2hex
char int2hex(int v)
{
char hextable[]="0123456789ABCDEF";
v &amp;= 0xf;
return hextable[v];
}
///////////////////
function int2hex(v:integer):string;
begin
result := inttohex(v,0);
end;

//解密
int DeCode(char *in, char *out)
{
if (in == NULL)
return 0;
int lcnt = strlen(in) >> 1;
if (lcnt == 0)
return 0;
if (in[0] == (char)0)
return 0;
int i = 0;
int icode = 0;
int _edi = 0xf667;
for (i = 0
i < lcnt
i++)
{
out = ((hex2int(ucase(in[i *2])) &amp;0xf) << 4);
out += hex2int(ucase(in[i *2+1])) &amp;0xf;
icode = ((int)out) &amp;0xff;
out ^= ((_edi &amp;0xffff) >> 8);
_edi = (((_edi + icode) *0xce6d) &amp;0xffff) + 0x58bf;
}
out[lcnt] = (char)0;
return 1;
}

//加密
int EnCode(char *in, char *out)
{
int lcnt = strlen(in);
int i = 0;
int icode = 0;
int tcode = 0;
int hbyte = 0;
int lbyte = 0;
int _edi = 0xf667;
for (i = 0
i < lcnt
i++)
{
icode = ((int)in) &amp;0xff;
tcode = icode;
tcode ^= ((_edi &amp;0xffff) >> 8);
hbyte = (tcode &amp;0xf0) >> 4;
lbyte = (tcode &amp;0xf);
out[i *2] = int2hex(hbyte);
out[i *2+1] = int2hex(lbyte);
_edi = (((_edi + tcode) *0xce6d) &amp;0xffff) + 0x58bf;
}
out[i *2] = '/0';
return 1;
}

请问这里面的 EnCode 和 DeCode 怎样翻译???
 
也太长了,我很懒的
 
//解密
int DeCode(char *in, char *out)
{
if (in == NULL)
return 0;
int lcnt = strlen(in) >> 1;
if (lcnt == 0)
return 0;
if (in[0] == (char)0)
return 0;
int i = 0;
int icode = 0;
int _edi = 0xf667;
for (i = 0
i < lcnt
i++)
{
out = ((hex2int(ucase(in[i *2])) &amp;0xf) << 4);
out += hex2int(ucase(in[i *2+1])) &amp;0xf;
icode = ((int)out) &amp;0xff;
out ^= ((_edi &amp;0xffff) >> 8);
_edi = (((_edi + icode) *0xce6d) &amp;0xffff) + 0x58bf;
}
out[lcnt] = (char)0;
return 1;
}

//加密
int EnCode(char *in, char *out)
{
int lcnt = strlen(in);
int i = 0;
int icode = 0;
int tcode = 0;
int hbyte = 0;
int lbyte = 0;
int _edi = 0xf667;
for (i = 0
i < lcnt
i++)
{
icode = ((int)in) &amp;0xff;
tcode = icode;
tcode ^= ((_edi &amp;0xffff) >> 8);
hbyte = (tcode &amp;0xf0) >> 4;
lbyte = (tcode &amp;0xf);
out[i *2] = int2hex(hbyte);
out[i *2+1] = int2hex(lbyte);
_edi = (((_edi + tcode) *0xce6d) &amp;0xffff) + 0x58bf;
}
out[i *2] = '/0';
return 1;
}

不长的,其实真正要翻译的就是几行,不过我翻译的总是不对,没办法只好到这来求教了
 
//解密
function DeCode(InStr:Pchar;var OutStr:Pchar):Integer;
var lcnt,I,icode,_edi:Integer;
S:String[2];
begin
Result:=0;
if not Assigned(InStr) then Exit;
lcnt:=strlen(InStr);
lcnt:=lcnt shr 1;
if lcnt=0 then Exit;
if InStr[0]=#0 then Exit;
icode:=0;
_edi:=$f667;
for i:=0 to lcnt-1 do
begin
S[1]:=InStr[I*2];
S[2]:=InStr[I*2+1];
icode:=StrToInt('$'+S);
OutStr:=Chr(icode xor (_edi shr 8));
_edi:=(((_edi+icode)*$ce6d) and $ffff)+$58bf;
end;
OutStr[lcnt]:=#0;
Result:=1;
end;

//加密
function EnCode(InStr:Pchar;var OutStr:Pchar):Integer;
var lcnt,I,icode,tcode,_edi:Integer;
S:String;
begin
lcnt:=strlen(InStr);
icode:=0;
tcode:=0;
_edi:=$f667;
for i:=0 to lcnt-1 do
begin
icode:=Ord(InStr);
icode:=(iCode xor (_edi shr 8))and $FF;
S:=IntToHex(icode,2);
outStr[i*2]:=S[1];
outStr[i*2+1]:=S[2];
_edi:=(((_edi+icode)*$ce6d)and $ffff)+$58bf;
end;
outStr[i*2]:=#0;
Result:=1
end;


procedure TForm1.Button1Click(Sender: TObject);
var P:Pchar;
S:String;
begin
GetMem(P,100);
S:='Button1Click';
EnCode(Pchar(S),P);
S:=P;
ShowMessage(S);
DeCode(Pchar(S),P);
S:=P;
ShowMessage(S);
FreeMem(P);
end;
 
不好意思刚才搞错了,用 刘浏 这个帐号发帖子了,
等下结帐时候分数给 麻子 ,不要给 刘浏 (都是我)
 
麻子兄果然厉害,佩服佩服!!
 
后退
顶部