简体   繁体   中英

How to use HextoBin in Delphi Firemonkey for Android

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);

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);
  ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM