//注意:Delphi2010以下版本默认的字符编码是ANSI,VS2010的默认编码是UTF-8,delphi版字符串事先须经过AnsiToUtf8()转码才能跟C#版得到的十六进制字符串显示结果一致。

Delphi版:

function StrToHex(AStr: string): string;
var
i : Integer;
ch:char;
begin

  Result:='';
  for i:=1 to length(AStr)  do
  begin
    ch:=AStr[i];
    Result:=Result+IntToHex(Ord(ch),2);
  end;
end;


 

//***************************************************

C#版

   

 public string StrToHex(string str)
    {
        string strResult;
        byte[] buffer = Encoding.GetEncoding("utf-8").GetBytes(str);
        strResult = "";
        foreach (byte b in buffer)
        {
            strResult += b.ToString("X2");//X是16进制大写格式 
        }
        return strResult;
    }

 

posted on 2014-05-28 10:44  曼波  阅读(11133)  评论(0编辑  收藏  举报