void SetCheckSum(unsigned char* temp,int Length){       unsigned long t;       unsigned char x;       int i,j;        *(((unsigned char *)&t)+1)=*temp;       *(((unsigned char *)&t)+2)=*(temp+1);       for(j=2;j<Length;j++){              t=t&0x00ffff00L;              *(((unsigned char *)&t)+3)=*(temp+j);              for(i=0;i<8;i++){                     t=t>>1;                     x=(unsigned char)((t&0x00000080L)>>7);                     if(x==1)                            t=t^0x00A00100L;              }       }        *(temp+Length-2)=(unsigned char)((t&0x0000ff00)>>8);       *(temp+Length-1)=(unsigned char)((t&0x00ff0000L)>>16);  }

解决方案 »

  1.   

    procedure SetCheckSum(temp: PByte; Length: Integer);
    var
      t:   LongWord;
      x:   byte;
      i,j: Integer;
      pb1, pTemp: PByte;
    begin
      pTemp := temp;
      pb1 := PByte(@t);
      Inc(pb1);
      pb1^ := pTemp^;
      Inc(pb1);
      Inc(pTemp);
      pb1^ := pTemp^;
      Inc(pb1);
      pTemp := temp;  for j := 2 to Length - 1 do
      begin
         t := t and $00ffff00;
         Inc(pTemp, j);
         pb1^ := pTemp^;
         for i := 0 to 7 do
         begin
      t := t shl 1;
            x := BYTE((t and $00000080) shl 7);
            if x = 1 then
              t := t xor $00A00100;
         end;
      end;
      
      pTemp := temp;
      Inc(pTemp, Length-1);
      pTemp^ := BYTE((t and $0000ff00) shl 8);
      Inc(pTemp);
      pTemp^ := BYTE((t and $00ff0000) shl 16);
    end;delphi指針操作很不方便 aaaa
      

  2.   

    delphi中可以嵌入汇编语言,要是能在嵌入其它语言更爽了
      

  3.   

    这段小程序完全可以用嵌入汇编来写,估计要比上面的那位老兄翻译成PASCAL用的代码还少。
      

  4.   

    procedure SetCheckSum(temp:pchar;Length:integer);
    asm
      push ebx   //寄存器  mov ebx,temp
      mov ax,[ebx]
      shl eax,8  mov ecx,2
    @c1:
      and eax,$00FFFF00
      rol eax,8
      mov al,[ebx+ecx]
      ror eax,8
      xor edx,edx
    @c2:
      shr eax,1
      test eax,$80
      jz @c3
      xor eax,$00A00100
    @c3:
      inc edx
      cmp edx,8
      jl  @c2
      inc ecx
      cmp ecx,Length
      jl  @c1  add ebx,length
      sub ebx,2  shr eax,8
      mov [ebx],ax  pop ebx
    end;
    楼上写的不能编译呀。
      

  5.   

    TO:  beyondtkl(大龙驹<*百善孝为先*>) 怎么在编译时,提示出错:
    错误行:pTemp^:= BYTE((t and $00ff0000) shl 16);
    错误提示:Constant expression violates subrange bounds
      

  6.   

    人家明明是>>右移,哪个家伙写 shl 左移啊?
      

  7.   

    to: Hunto(恶魔猎手) 你的也不行啊,运行有错误!
      

  8.   

    procedure SetCheckSum(temp: PByte; Length: Integer);
    var
      t, i, j: Integer;
      x: Byte;
    begin
      PByte(Integer(@t)+1)^ := temp^;
      //     *(((unsigned char *)&t)+1)=*temp;
      PByte(Integer(@t)+2)^ := temp^;
      //     *(((unsigned char *)&t)+2)=*(temp+1);
      for j:=2 to Length-1 do
      begin
        t := t and $00ffff00;
        PByte(Integer(@t)+3)^ := PByte(Integer(temp)+j)^;
        // *(((unsigned char *)&t)+3)=*(temp+j);
        for i:=0 to 7 do
        begin
          t := t shr 1;
          x := Byte((t and $00000080) shr 7);
          //(unsigned char)((t&0x00000080L)>>7);
          if x = 1 then
            t := t xor $00A00100;
        end;
      end;
      PByte(Integer(temp)+Length-2)^ := Byte((t and $0000ff00) shr 8);
      //    *(temp+Length-2)=(unsigned char)((t&0x0000ff00)>>8);
      PByte(Integer(temp)+Length-1)^ := Byte((t and $00ff0000) shr 16);
      //    *(temp+Length-1)=(unsigned char)((t&0x00ff0000L)>>16);
    end;
      

  9.   

    抱歉!更正begin后第3行为:
    PByte(Integer(@t)+2)^ := PByte(Integer(temp)+1)^;
      

  10.   

    TO:DDGG(叮叮当当编译到没问题,好像算出的数据,不对啊!
      

  11.   

    procedure SetCheckSum1(temp:pchar;Length:integer);stdcall;
    asm
      push ebx     mov ebx,temp
      mov ax,[ebx]
      shl eax,8  mov ecx,2
    @c1:
      and eax,$00FFFF00
      rol eax,8
      mov al,[ebx+ecx]
      ror eax,8
      xor edx,edx
    @c2:
      shr eax,1
      test eax,$80
      jz @c3
      xor eax,$00A00100
    @c3:
      inc edx
      cmp edx,8
      jl  @c2
      inc ecx
      cmp ecx,Length
      jl  @c1  shr eax,8
      add ebx,length
      dec ebx
      dec ebx  mov [ebx],ax
      pop ebx
    end;
    这个应该可以了。算出来的值和那位老兄的一样了。上一次算法是对的。只是没考虑到Delphi传递参数是放在寄存器里,所以造成算出来不对,甚至于报错。
      

  12.   

    偶写的不是翻译而是意译。如: 
       x=(unsigned char)((t&0x00000080L)>>7);
       if(x==1)t=t^0x00A00100L;  
    翻译成: test eax,$80 是非常合理的。
    实际上这一句写得有点罗嗦,而应写
      if(t & 0x80) t=t^0xA00100;
    换成Delphi写成:
      if (t and $80)<>0 then t:= t xor $A00100;
    就行了。
      

  13.   

    纯正的Pascal版
    procedure SetCheckSum(temp: Pchar; Length: Integer);
    var
      t,i,j:integer;
    begin
      Pchar(Integer(@t)+1)^ := temp[0];
      Pchar(Integer(@t)+2)^ := temp[1];
      for j:=2 to Length-1 do
      begin
        t := t and $00ffff00;
        pchar(Integer(@t)+3)^ :=temp[j];
        for i:=0 to 7 do
        begin
          t := t shr 1;
          if (t and $00000080)<>0 then t := t xor $00A00100;
        end;
      end;
      temp[Length-2]:=char(t shr 8 and $FF );
      temp[Length-1]:=char(t shr 16 and $FF);
    end;
      

  14.   

    To: CoolDesigner(中原一剑) 
    Hunto(恶魔猎手) 写的比我更精简,但是计算结果是同我一样的。
    我在想,如果不正确的话,会不会这段程序本身就有问题呢?你是怎样检验的?
      

  15.   

    再贴一个汇编的,(在Delphi 7 下编译通过,执行结果和前面完全一样)。
    根据Delphi参数传递规则。第一个参数传EAX,第二个放在EDX,进行优化,使代码更少,速度更快。
    procedure SetCheckSum(temp:pchar;Length:integer);
    asm
      push esi
      mov esi,eax
      add edx,esi
      cld
      lodsw
      shl eax,8
    @c1:
      and eax,$00FFFF00
      rol eax,8
      lodsb
      ror eax,8
      xor ecx,ecx
    @c2:
      shr eax,1
      test eax,$80
      jz @c3
      xor eax,$00A00100
    @c3:
      inc ecx
      cmp ecx,8
      jl  @c2
      cmp esi,edx
      jl  @c1
      shr eax,8
      mov [edx-2],ax
      pop esi
    end;
      

  16.   

    对于楼上说的 Delphi的指针不太好用,不敢苟同,以前是没用过。现在用完之后感觉完全和C一样好用。
    其实Pchar(Integer(@t)+1)^ := temp[0];完全可以写成 :Pchar(@t)[1]:=temp[0];
    这样就简单多了。