delphi 汉字转十六进制的函数,可以互转不乱码  
官方Delphi 学习QQ群: 682628230(三千人)
频道

delphi 汉字转十六进制的函数,可以互转不乱码


Function HexToStr(S: String): String;
Var
  Stream: TMemoryStream;
  Value: TStringStream;
  Pos: Integer;
Begin
  Result := '';
  If Length(S) > 0 Then
  Begin
    Stream := TMemoryStream.Create;
    Value := TStringStream.Create('');
    Try
      Pos := Stream.Position;
      Stream.SetSize(Stream.Size + Length(S) Div 2);
      HexToBin(PChar(S), PChar(Integer(Stream.Memory) + Stream.Position), Length(S) Div 2);
      Stream.Position := Pos;
      Value.CopyFrom(Stream, Length(S) Div 2);
      Result := Value.DataString;
    Finally
      Stream.Free;
      Value.Free;
    End;
  End;
End;

Function StrToHex(S: String): String;
Var
  Stream: TMemoryStream;
  Value: TStringStream;
Begin
  If Length(S) > 0 Then
  Begin
    Value := TStringStream.Create(S);
    Try
      SetLength(Result, (Value.Size - Value.Position) * 2);
      If Length(Result) > 0 Then
      Begin
        Stream := TMemoryStream.Create;
        Try
          Stream.CopyFrom(Value, Value.Size - Value.Position);
          Stream.Position := 0;
          BinToHex(PChar(Integer(Stream.Memory) + Stream.Position), PChar(Result), Stream.Size - Stream.Position);
        Finally
          Stream.Free;
        End;
      End;
    Finally
      Value.Free;
    End;
  End;
End;

来源:https://bbs.csdn.net/topics/390876064


推荐分享
图文皆来源于网络,内容仅做公益性分享,版权归原作者所有,如有侵权请告知删除!
 

Copyright © 2014 DelphiW.com 开发 源码 文档 技巧 All Rights Reserved
晋ICP备14006235号-8 晋公网安备 14108102000087号

执行时间: 0.12031197547913 seconds