How to use HextoBin in Delphi Firemonkey for Andro

2019-09-17 02:30发布

I'm trying to adopt some Windows code to Android, but I am unsuccessful.

When I try to compile the following code, I get an error:

[DCC Error] There is no overloaded version of 'HexToBin' that can be called with these arguments

var
  BinaryStream: TMemoryStream;
  HexStr: String;
  WSocket: TWSocket;
begin
  HexStr := memo1.Text;
  BinaryStream := TMemoryStream.Create;
  try
    BinaryStream.Size := Length(HexStr) div 2;
    if BinaryStream.Size > 0 then
    begin
      HexToBin(PChar(HexStr), BinaryStream.Memory, BinaryStream.Size);
      HexStr:='';
      HexStr:=MemoryStreamToString(BinaryStream);
      memo1.Text:=HexStr;
      IdUDPClient1.Send(HexStr);

1条回答
smile是对你的礼貌
2楼-- · 2019-09-17 02:47

For Android you have to use one of these overloads:

function HexToBin(const Text: PChar; TextOffset: Integer;
  var Buffer: TBytes; BufOffset: Integer; Count: Integer): Integer; overload;

function HexToBin(const Text: TBytes; TextOffset: Integer;
  var Buffer: TBytes; BufOffset: Integer; Count: Integer): Integer; overload;

You can best achieve this by using a TBytesStream instead of a TMemoryStream.

A valid call could then look like this:

var
  BinaryStream: TBytesStream;
  bytes: TBytes;
  HexStr: String;
begin
  HexStr := memo1.Text;
  SetLength(bytes, Length(HexStr) div 2);
  HexToBin(PWideChar(HexStr), 0, bytes, 0, Length(bytes));
  BinaryStream := TBytesStream.Create(bytes);
  ...
查看更多
登录 后发表回答