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

相关阅读 >>

Delphi 利用tdictionary 文本去重

Delphi 日期时间函数

Delphi tdictionary 泛型如何排序

Delphi qq尾巴病毒的编写

Delphi利用scrollbox实现图像漫游

Delphi tgifimage:timage显示gif动画

Delphi firemonkey处理图形的方式与vcl处理图形的方式大不相同

Delphi 判断字符是否是汉字,bytetype字符串中判断是否英文

Delphi datamodule1 fdconnection1数据库连接

Delphi 循环 low high 用法

更多相关阅读请进入《Delphi》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...