如何使用Delphi将字节数组转换为字符串? [英] How do I convert an array of bytes to string with Delphi?

查看:254
本文介绍了如何使用Delphi将字节数组转换为字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用Delphi开发一个项目,我想将字节数组转换为字符串类型.我该怎么办?

I am developing a project with Delphi and I want to convert the byte array to string type. How can I do?

示例C#代码:

private void ListenerOnDataTransmit(DataTransmitEventArgs e)
{
    transmittedMsg = BitConverter.ToString(e.TransmittedBytes, 0, e.TransmittedBytes.Length);
    try { Invoke(new EventHandler(UpdateTransmittedMessagesListView)); }
    catch { }
}

推荐答案

SysUtils.IntToHex()函数在Delphi 7中手动执行相同的操作,例如:

The BitConverter.ToString() method "Converts the numeric value of each element of a specified array of bytes to its equivalent hexadecimal string representation." You can do the same thing manually in Delphi 7 by using the SysUtils.IntToHex() function in a loop, eg:

uses
  ..., SysUtils;

var
  bytes: array of byte;
  s: string;
  i: Integer;
begin
  bytes := ...;
  s := '';
  if bytes <> nil then
  begin
    s := IntToHex(bytes[0], 2);
    for i := 1 to High(bytes) do
      s := s + '-' + IntToHex(bytes[i], 2);
  end;
end;

这篇关于如何使用Delphi将字节数组转换为字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆