karl Jonson 0 Posted November 10, 2020 (edited) Hi, I would like to compare byte array to ansichar. If MyByteArray = MyansiChar then ... Which is the best method to do that ? TA Edited November 10, 2020 by karl Jonson spelling Share this post Link to post
David Heffernan 2345 Posted November 10, 2020 Can you be precise about the types here. At the moment all we know is the name of the variables. 1 Share this post Link to post
karl Jonson 0 Posted November 10, 2020 7 minutes ago, David Heffernan said: Can you be precise about the types here. At the moment all we know is the name of the variables. The types used are: array[0..12] of Byte system.ansichar TA Share this post Link to post
Anders Melander 1782 Posted November 11, 2020 Assuming you meant AnsiString and not AnsiChar (it doesn't make sense to compare 13 bytes to 1 ansichar): function Compare(const Bytes: TByteArray; const Str: AnsiString): boolean; begin Result := (Length(Str) = SizeOf(Bytes)) and (CompareMem(@Str[1], @Bytes[0], SizeOf(Bytes)); end; Share this post Link to post
karl Jonson 0 Posted November 11, 2020 26 minutes ago, Anders Melander said: Assuming you meant AnsiString and not AnsiChar (it doesn't make sense to compare 13 bytes to 1 ansichar): function Compare(const Bytes: TByteArray; const Str: AnsiString): boolean; begin Result := (Length(Str) = SizeOf(Bytes)) and (CompareMem(@Str[1], @Bytes[0], SizeOf(Bytes)); end; Thanks. It's ansichar type. I need to check if MyByteArray = MyansiChar (eg. control character EOT) Share this post Link to post
Anders Melander 1782 Posted November 11, 2020 1 minute ago, karl Jonson said: It's ansichar type I need to check if MyByteArray = MyansiChar (eg. control character EOT) As I wrote you cannot compare a byte (ansichar) to 13 bytes (byte array) for equality. Since the size is different they will always be different, so what you're asking isn't making sense. Are you trying to determine if the byte array contains the ansichar? function Contains(const Bytes: TByteArray; c: AnsiChar): boolean; begin for var b in Bytes do if (b = Ord(c)) then Exit(True); Result := False; end; Share this post Link to post
David Heffernan 2345 Posted November 11, 2020 Can you describe how you want to perform this comparison? How you want to compare the 13 bytes in the array with the single byte in the AnsiChar variable. Share this post Link to post