语言差异太大了,
int UnGateCode( char * in, char * out )
{
if( in == NULL )
return 0;
int lcnt = strlen( in ) >> 1;
if( lcnt == 0 )
return 0;
if( in[0] == (char)0)//'\0' )
return 0;
int i = 0;
int icode = 0;
int _edi = 0xf667; for( i = 0;i < lcnt;i ++ )
{
out[i] = ((hex2int( ucase( in[i * 2])) & 0xf )<<4);
out[i] += hex2int( ucase( in[i * 2 + 1])) & 0xf;
icode = ((int)out[i]) & 0xff;
out[i] ^= ((_edi & 0xffff)>>8);
_edi = (((_edi + icode ) * 0xce6d ) & 0xffff ) + 0x58bf; } out[lcnt] = (char)0;//'\0'; return 1;
}

解决方案 »

  1.   

    function UnGateCode(in,out:String):integer;
    var
      i,icode,edi,lcnt:integer; 
    begin
      if in=null then
        Result:=0;
      lcnt:=lenght(in);
      if lcnt=0 then
        Result:=0;
      if in[0]='\0' then
       Result:=0;
      i:=0;edi = 0xf667
      iCode:=0;  for i:=0 to lcnt-1 do
      begin
        out[i]:=((hex2int( ucase( in[i * 2])) & 0xf )<<4);
        out[i] =out[i]+ hex2int( ucase( in[i * 2 + 1])) & 0xf;
        icode = ((int)out[i]) & 0xff;
    ........................  end;
        
    end;
      

  2.   

    function UnGateCode( in:pchar, out:pchar ):integer;
    var
    lcnt,i,icode,_edi:integer;

    begin
    if in = NULL  then
    begin
    result:=0;
    exit;
    end;
    lcnt = length( in ) shr 1;
    if lcnt = 0 then
    begin
    result:=0;
    exit;
    end;
    if in[0] = ord(0) then
    begin
    result:=0;
    exit;
    end;
     i := 0;
     icode := 0;
    _edi := $f667; for i := 0 to lcnt do
    begin
    out[i] := ((hex2int( ucase( in[i * 2])) and $f ) shl 4);
    out[i] :=inc(out[i],hex2int( ucase( in[i * 2 + 1])) and $f);
    icode := ((intger)out[i]) and $ff;
    out[i]^:= ((_edi and $ffff)  shr 8);
    _edi := (((_edi + icode ) * $ce6d ) and $ffff ) + $58bf; end; out[lcnt] = ord(0); result:=1;
    end;
      

  3.   

    function UnGateCode(Tin: PChar; Tout: PChar):integer;
    var
      lcnt,i,icode,_edi: integer;
      p: pointer;
    begin
      if Tin = nil then
      begin
        result := 0;
        exit;
      end;
      lcnt := Length(Tin) shr 1;
      if Tin[0] = Char(0) then
      begin
        result := 0;
        exit;
      end;
      i := 0;
      icode := 0;
      _edi := $f667;
      p := Pointer((_edi and $ffff) shr 8);
      for i:=0 to lcnt do
      begin
      Tout[i] := Char((integer(UpCase( Tin[i * 2])) and $f ) shl 4);
    Tout[i] := Char(integer(Tout[i]) + integer( UpCase( Tin[i * 2 + 1])) and $f);
    icode := integer(Tout[i]) and $ff;
    Tout[i] := Char(P^);    //out[i] ^= ((_edi & 0xffff)>>8);不知道是什么意思,瞎转换的!
    _edi := (((_edi + icode ) * $ce6d ) and $ffff ) + $58bf;
      end;
      Tout[lcnt] := char(0);
      result := 1;
    end;
      

  4.   

    此类涉及到移位,位计算(与、或、异或)的函数,用C作当然最合适了,为什么非得用Pascal实现?
    将函数作成dll或者ocx然后调用就非常简单了。
    呵呵out[i] ^= ((_edi & 0xffff)>>8);
    ==>
    out[i] = (out[i]) xor ((_edi & 0xffff)>>8); // Xor 指位异或, pascal没有对应的函数
      

  5.   

    上面的各位,忘了说明一下,原程序是编译通过且计算无误:
    1,上面的int2hex是个自定义函数相当于strtoint('$'+hexstr)
        ucase是把小写字母转成大写!!
    现在我不能理解的是:
    1,out既然是pchar,out[i]又怎么会等于int类型?最不明白的!!!!!
    2,out是字符串的话怎么会有out[0],字符串不是从1开始吗?所以原c程序中在
      循环完了后又加了个char(0)。我个人觉得循环应该从1开始
      c中字符串结束是‘\0’,delphi中是什么?
      

  6.   

    你自己多调试吧,
    在C中,数组缺省的下标是从0开始,Delphi字符串中的字符下标从1开始,如果是短字符串,则读[0]为长度。
    '\0'为Chr(0),或者#0
    字符转成整数为Ord(AChar);
      

  7.   

    to  leeky(雅痞·千年虫) :
    按你的说法那楼上的循环从0开始是错的。
    在c中可以把一个integer赋值给char* out吗?(见循环中的前两行)
      

  8.   

    --1,out既然是pchar,out[i]又怎么会等于int类型?最不明白的!!!!!因为C/C++可以把char为成是一个短的整型,在C语言中经常是这样用的, 另外在C语言中还把所有0看成是false , 非0看成是true , 所以经常会有:
    for (i = 0 ; str[i]; i++) {
    ....
    ...
    }
    这样的循环语句, 总之,在C里bool, char, int 都是"整型"可以混用.--2,out是字符串的话怎么会有out[0],字符串不是从1开始吗?所以原c程序中在
      循环完了后又加了个char(0)。我个人觉得循环应该从1开始
      c中字符串结束是‘\0’,delphi中是什么?
    在C中字符串是以0开始的, 是值0结束的也就是 '\0'了,或者干脆0就行了,反正都一样.