function HexToStr(const S: String): String;
var
  i: Integer;
  t: Byte;
begin
  SetLength(Result, Length(S) div 2);
  FillChar(PChar(Result)^, Length(Result), 0);
  for i := 1 to Length(S) div 2 * 2 do
  begin
    if (S[i] >= 'a') and (S[i] <= 'z') then
      t := Ord(S[i]) - Ord('a') + 10
    else if (S[i] >= 'A') and (S[i] <= 'Z') then
      t := Ord(S[i]) - Ord('A') + 10
    else if (S[i] >= '0') and (S[i] <= '9') then
      t := Ord(S[i]) - Ord('0')
    else
      t := 0;
    if i mod 2 <> 0 then t := t shl 4;    Byte(Result[(i + 1) div 2]) :=Byte(Result[(i + 1) div 2]) or Byte(t);
    //上面这行代码为什么可以这么赋值,在delphi7下可以编译,在delphi2010中无法编译 ,求解。
  end;  
end;

解决方案 »

  1.   

    神奇,虽然大概理解这个意思,但觉得这种赋值方式不好,可以修改成如何:
    Result[(i + 1) div 2] := Chr(Byte(Result[(i + 1) div 2]) or Byte(t));
      

  2.   

    Byte(Result[(i + 1) div 2]) :=Byte(Result[(i + 1) div 2]) or Byte(t);
    该改为:
    Result[(i + 1) div 2] := chr( Byte(Result[(i + 1) div 2]) or Byte(t) );
      

  3.   

    DELPHI自带函数IntToHex可实现这个功能
      

  4.   

    类型不匹配造成的 改成这样
    function HexToStr(const S: ansiString): ansiString;
      

  5.   

    我觉得Byte(Result[(i + 1) div 2]) :=Byte(Result[(i + 1) div 2]) or Byte(t);这句就像汇编一样:
    mov byte ptr result[(i+1) div 2]),byte ptr[右边],
    那个Byte()只是用来指定传输大小的,和byte ptr一个道理