Jump to content
Sign in to follow this  
karl Jonson

Compare byte array to ansichar

Recommended Posts

Hi,
I would like to compare byte array to ansichar.
 

If MyByteArray = MyansiChar then  
  ...

Which is the best method to do that ?

TA

Edited by karl Jonson
spelling

Share this post


Link to post
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

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

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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×