function HexToStr(mHex: string): string;
var
  I: Integer;
begin
  for I := 1 to Length(mHex) div 2 do
    Result := Result + Chr(StrToIntDef('$' + mHex[I * 2 - 1] + mHex[I * 2], 0)); 
end; { HexToStr }function StrToHex(mStr: string): string;
var
  I: Integer;
begin
  Result := '';
  for I := 1 to Length(mStr) do
    Result := Format('%s%.2x', [Result, Ord(mStr[I])]);
end; { StrToHex }

解决方案 »

  1.   

    function HexToStr(mHex: string): string;
    var
      I: Integer;
    begin
      Result := ''; //赋一个初值安全点
      for I := 1 to Length(mHex) div 2 do
        Result := Result + Chr(StrToIntDef('$' + mHex[I * 2 - 1] + mHex[I * 2], 0));
    end; { HexToStr }
      

  2.   

    function StrToHex(mStr: string): string;
    var
      I: Integer;
    begin
      Result := '';
      for I := 1 to Length(mStr) do
        Result := Format('%s%%%.2x', [Result, Ord(mStr[I])]); //%%->%
    end; { StrToHex }
      

  3.   

    //from
    http://kingron.myetang.com/zsfunc0q.htm
    //请参考function StringToDisplay(mString: string): string;
    var
      I: Integer;
      S: string;
    begin
      Result := '';
      S := '';
      for I := 1 to Length(mString) do
        if mString[I] in [#32..#127] then
          S := S + mString[I]
        else begin
          if S <> '' then begin
            Result := Result + QuotedStr(S);
            S := '';
          end;
          Result := Result + Format('#$%x', [Ord(mString[I])]);
        end;
      if S <> '' then Result := Result + QuotedStr(S);
    end; { StringToDisplay }function DisplayToString(mDisplay: string): string;
    var
      I: Integer;
      S: string;
      B: Boolean;
    begin
      Result := '';
      B := False;
      mDisplay := mDisplay;
      for I := 1 to Length(mDisplay) do
        if B then case mDisplay[I] of
          '''': begin
            if S <> '' then Result := Result + StringReplace(S, '''''', '''', [rfReplaceAll]);
              if Copy(mDisplay, I + 1, 1) = '''' then Result := Result + '''';
              S := '';
              B := False;
            end;
          else S := S + mDisplay[I];
          end
        else case mDisplay[I] of
          '#', '''': begin
            if S <> '' then Result := Result + Chr(StrToIntDef(S, 0));
            S := '';
            B := mDisplay[I] = '''';
          end;
          '$', '0'..'9', 'a'..'f', 'A'..'F': S := S + mDisplay[I];
        end;
      if (not B) and (S <> '') then Result := Result + Chr(StrToIntDef(S, 0));
    end; { DisplayToString }
      

  4.   

    //==============================================================================
    //字符串转换为十六进制字符串:例如‘ABCD’->‘41424344’*************************
    //==============================================================================
    function BinaryToHexStr(Str:string):string;
    var i: integer;
    begin
      Result := '';
      for i:=1 to Length(Str) do Result := Result + IntToHex(Ord(Str[i]),2);
    end;//==============================================================================
    //十六进制字符串转换为字符串:例如‘41424344’->‘ABCD’*************************
    //==============================================================================
    function HexStrToBinary(Str:string):string;
    var i, Len: integer;
        Point: Pointer;
    begin
      Len := Length(Str) div 2;
      Point := AllocMem(Len + 1);//Len + 1是为nil结尾的标志空出一个字节
      Pointer(Result) := Point;
      for i:=0 to Len do Result[i]:= Chr(StrToint('$' + Copy(Str, i*2-1, 2)));
      ReallocMem(Point, Len + 1);
    end;