memcmp and memcpy in c# ?

  • Thread starter Thread starter Alex Stark
  • Start date Start date
A

Alex Stark

Hi,

I'm new to c# an missing functions like memcmp and memcpy from C.
Is there something similar ?

cu
Alex
 
Alex,

To copy value types (such as structures) what you need to do is just using
the assignment operator; this will make a shallow copy of the struct
members.

I would say almost all reference types that represent block of data in the
memory (e.g. arrays and collections) CopyTo
method. Actually this method is defined in the ICollection interface.

The other way you can create a copy of an object's data is to call Clone if
the type implements ICloneable interface.

In addition there is a method defined on the level of the Object class
called MemberwiseClone. This mehod creates a shallow copy of the object. The
method is protected and is meant to be used for implementing *clone*.

As far as string comparison goes you can use the CompareXXXX methods of
the String class.
 
Hi Stoicho,

thanks for your answer. But I don't really understand what you mean.
All I have is a bytearray b1 and I want to kow is, if a second bytearray b2
is part somewhere in b1.

In C the code is a simple :

int section_init(uchar *tra, int tralen)
{
int i, j=0;
uchar buf[256];

if (memcmp(tra+4, "LOCKED", 6))
return(0);
...................
}

How is such a simple think in C# realizable ? Sure, I can build a string
and in this case this will be the easiest way.....but I think, there are
some
more complex issues, where string conversation will not the best way.

Thanks for your help !!!!!!!!!!!

Alex
 
Hi Stoicho,

thanks for your answer. But I don't really understand what you mean.
All I have is a bytearray b1 and I want to kow is, if a second
bytearray b2 is part somewhere in b1.

In C the code is a simple :

int section_init(uchar *tra, int tralen)
{
int i, j=0;
uchar buf[256];

if (memcmp(tra+4, "LOCKED", 6))
return(0);
..................
}

How is such a simple think in C# realizable ? Sure, I can build a
string and in this case this will be the easiest way.....but I think,
there are some
more complex issues, where string conversation will not the best way.

Thanks for your help !!!!!!!!!!!

Alex


Stoitcho Goutsev (100) said:
Alex,

To copy value types (such as structures) what you need to do is just
using the assignment operator; this will make a shallow copy of the
struct members.

I would say almost all reference types that represent block of data
in the memory (e.g. arrays and collections) CopyTo
method. Actually this method is defined in the ICollection interface.

The other way you can create a copy of an object's data is to call
Clone if the type implements ICloneable interface.

In addition there is a method defined on the level of the Object
class called MemberwiseClone. This mehod creates a shallow copy of
the object. The method is protected and is meant to be used for
implementing *clone*.

As far as string comparison goes you can use the CompareXXXX
methods of the String class.

Have a look at the Buffer class.
 
Alex,

In .NET array of bytes is different than array of chars which is totally
different than strings. Further mode Char is 16 bit where Byte is 8.
If you want to look for a sub string use strings. Otherwise you
need to write your own code for lookup consecutive bytes in a byte array.

There is no equivalent of memcmp in managed framework.

--
HTH
Stoitcho Goutsev (100)

Alex Stark said:
Hi Stoicho,

thanks for your answer. But I don't really understand what you mean.
All I have is a bytearray b1 and I want to kow is, if a second bytearray
b2
is part somewhere in b1.

In C the code is a simple :

int section_init(uchar *tra, int tralen)
{
int i, j=0;
uchar buf[256];

if (memcmp(tra+4, "LOCKED", 6))
return(0);
..................
}

How is such a simple think in C# realizable ? Sure, I can build a string
and in this case this will be the easiest way.....but I think, there are
some
more complex issues, where string conversation will not the best way.

Thanks for your help !!!!!!!!!!!

Alex
 
Stoitcho Goutsev (100) said:
In .NET array of bytes is different than array of chars which is totally
different than strings. Further mode Char is 16 bit where Byte is 8.
If you want to look for a sub string use strings. Otherwise you
need to write your own code for lookup consecutive bytes in a byte array.

There is no equivalent of memcmp in managed framework.

Note that there's Buffer.BlockCopy, which isn't really an equivalent,
but can sometimes be used in the same situations.
 
Hello Jon Skeet [C# MVP],
Note that there's Buffer.BlockCopy, which isn't really an equivalent,
but can sometimes be used in the same situations.

Note that memcmp = memory compare, as he's searching for a byte sequence
in a larger array.
Don't think there's anything present in the .NET framework that does exactly
that, at least neither Buffer or Array seems to have any methods that compare
parts of arrays against each other.
 
Lasse said:
Note that memcmp = memory compare, as he's searching for a byte sequence
in a larger array.

Oops - you're absolutely right. I misread Stoitcho's post. Doh!

Jon
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Back
Top