473,376 Members | 1,184 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,376 software developers and data experts.

memcmp and memcpy in c# ?

Hi,

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

cu
Alex

Jan 11 '06 #1
7 12702
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.

--
HTH
Stoitcho Goutsev (100)

"Alex Stark" <st****@online.nospam> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
Hi,

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

cu
Alex

Jan 11 '06 #2
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)" <10*@100.com> schrieb im Newsbeitrag
news:O6**************@TK2MSFTNGP12.phx.gbl...
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.

--
HTH
Stoitcho Goutsev (100)

"Alex Stark" <st****@online.nospam> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
Hi,

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

cu
Alex


Jan 11 '06 #3
"Alex Stark" <st****@online.nospam> wrote in
news:e4**************@TK2MSFTNGP10.phx.gbl:
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)" <10*@100.com> schrieb im Newsbeitrag
news:O6**************@TK2MSFTNGP12.phx.gbl...
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.

--
HTH
Stoitcho Goutsev (100)

"Alex Stark" <st****@online.nospam> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
Hi,

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

cu
Alex




Have a look at the Buffer class.
Jan 11 '06 #4
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" <st****@online.nospam> wrote in message
news:e4**************@TK2MSFTNGP10.phx.gbl...
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)" <10*@100.com> schrieb im Newsbeitrag
news:O6**************@TK2MSFTNGP12.phx.gbl...
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.

--
HTH
Stoitcho Goutsev (100)

"Alex Stark" <st****@online.nospam> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
Hi,

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

cu
Alex



Jan 11 '06 #5
Stoitcho Goutsev (100) <10*@100.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jan 11 '06 #6
Hello Jon Skeet [C# MVP],
Stoitcho Goutsev (100) <10*@100.com> wrote:
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.


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 Vågsæther Karlsen
http://usinglvkblog.blogspot.com/
mailto:la***@vkarlsen.no
PGP KeyID: 0x2A42A1C2
Jan 12 '06 #7
Lasse Våqsæther Karlsen wrote:
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.


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

Jan 12 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: rajkumar | last post by:
I have a struct like struct MyStruct { int a; int b; int c: bool d; bool e; }
4
by: blueblueblue2005 | last post by:
hi, I am reading the description of memcmp and strcmp, it seems there is no big difference between these two functions. except that memcmp takes void pointer parameters. so why string.h has these...
6
by: Sidney Cadot | last post by:
Hi all, Just browsing through my newly-acquired C99 spec, I was reading on the memcmp() function (7.21.4.1). It has a rather peculiar wording: int memcmp(const void *s1, const void *s2, size_t...
4
by: Preets | last post by:
Hi, Can anybody please tell me the difference between memcmp() and strncmp() functions ?
4
by: system55 | last post by:
which of the 2 commands are applicable in comparing an array of unsigned chars? if(strcmp(aAbsCylNumHigh, bAbsCylNumHigh)<=0 && strcmp(aAbsCylNumLow,bAbsCylNumLow)<=0 && strcmp(aSecNum,.bSecNum)<0...
39
by: Martin | last post by:
Please consider the following code fragment. Assume UINT32 is a typedef suitable for defining variables of 32 bits, and that ui32 is initialised. UINT32 ui32; /* ... */ /* assume ui32 now is...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.