Function Asc2Hex(Ans As String) As String
    Dim I As Long, tmpHex As String, RtnStr As String
    For I = 1 To Len(Ans)
        tmpHex = Hex(Asc(Mid(Ans, I, 1)))
        RtnStr = RtnStr & IIf(Len(tmpHex) = 1, "0" & tmpHex, tmpHex)
    Next
    Asc2Hex = RtnStr
End FunctionFunction Hex2Asc(Ans As String) As String
    Dim I As Long, tmpAsc As String
    For I = 1 To Len(Ans) Step 2
        If IsNumeric(Mid$(Ans, I, 1)) = True Then
            tmpAsc = tmpAsc & Chr$("&H" & Mid$(Ans, I, 2))
        Else
            tmpAsc = tmpAsc & Chr$("&H" & Mid$(Ans, I, 4))
            I = I + 2
        End If
    Next
    Hex2Asc = tmpAsc
End Function

解决方案 »

  1.   

    我没有完全按vb翻译,只是功能与上面的程序完全一致(我已作过简单测试)function Asc2Hex(Ans: AnsiString): AnsiString;
    var
    I: Longint;
     RtnStr: AnsiString;
    begin
    RtnStr:='';
    for I:=1 to Length(Ans) do begin
     RtnStr := RtnStr+InttoHex(ord(Ans[I]),2);
    end;
    Result := RtnStr;
    end;
    function Hex2Asc(Ans: AnsiString): AnsiString;
    var
    I: Longint;
    tmpAsc: AnsiString;
    begin
    I := 1;
    tmpAsc:='';
    while true do
    begin
       if I > Length(Ans) then
          break;
       tmpAsc := tmpAsc+chr(StrToInt('0x'+Copy(Ans, I, 2)));
       I := I + 2;
    end;
    Result := tmpAsc;
    end;
      

  2.   

    另外,delphi本身就有这两个函数,hextobin和bintohex
    ex:procedure TForm1.Button2Click(Sender: TObject);
    var
      buff1:array [0..20] of char;
      buff2:array [0..20] of char;
    begin
      fillchar(buff1,20,0);
      fillchar(buff2,20,0);  bintohex('你好123',buff1,7);
      Edit1.Text:=buff1;
      hextobin(buff1,buff2,7);
      Edit2.Text:=buff2;
    end;