Moving a string to char* and viceversa.

T

Tim Conner

Hi,

I am an ex-delphi programmer, and I having a real hard time with the
following simple code (example ):

Which is the equivalent to the following code ?

var
chars : PChar;
sBack, s : String;
begin
s := Tim Conner XXXXXXXXX';
chars := AllocMem(20);

Move( s[1], chars^, 20); <<-- This
sBack := chars; <<-- and this

ShowMessage(sBack);
FreeMem(chars, 20);


I could write a "move" function to put each character of the string into the
char*, eventhough I would prefer the more appropiate approach.
But what really worries me, is how to take the content of the PChar ( C# =
char* ) and put it back in to the string ??


Thanks in advance,
 
G

Greg Ewing [MVP]

Tim, do you want to go from string to char[] or char[] to string?

for string to char[] you can use string.ToCharArray();
for char[] to string you can use the string constructor which takes a
char[].

Hope that helps.
 
T

Tim Conner

Greg :

Thanks. I'll make it easier. I just need to know the equivalent to the
Delphi's "Move" procedure :

Delphi syntax:
procedure Move(const Source; var Dest; Count: Integer);

Description :
Move copies Count bytes from Source to Dest. No range checking is performed.
Move compensates for overlaps between the source and destination blocks.
Whenever possible, use the global SizeOf function (Delphi) or the sizeof
operator (C++) to determine the count.

Examples. Imagine you have a buffer in memory that contains chars and binary
data all together. You already know the offsets. So you use the"Move"
procedure to move a piece of the data to an specific data type :

var
Data : PChar; // this is equivalent to char* Data in C#.
IntegerValue : Integer; // This is equivalent to int IntegeValue
in C#
begin
// Buffer is also a PChar (Char*) definied somewhere else, with say a
size of 14,
// and we know we have an integer value starting at 10th position

Data := Buffer + 10; // Data is now pointing at 10th position;
Move( Data^, IntegerValue, SizeOf(Integer) ); // This moves 4 bytes
from Data to the IntegerValue
end;

So,
Data is the type of PChar(in Delphi), the same of Char* (in C#). The "^"
is the dereference operator, it takes the value from it.
IntegerValue is a variable of type Integer (in Delphi), the same of Int
(in C#).
SizeOf is very similar in both languages.


Then, which is C#'s equivalent to Delphi's "Move" ?


Thanks in advance,
 
R

Rudy Velthuis

Tim said:
Then, which is C#'s equivalent to Delphi's "Move" ?

There is no direct equivalent. Move is very low level (very much like the
Win32 API's MoveMemory and CopyMemory functions), and takes untyped var
parameters, which are a no-no in managed .NET. Move is very type-unsafe
as well.

Depending on your needs, you can use what Greg wrote, or
System.Array.Copy(), or String.ToCharArray(), or
System.Text.UnicodeEncoding.GetBytes(), or... or...

IOW, how this is done depends on the situation.
 
B

Bret Mulvey

Take a look at Array.Copy. It has different forms to allow you to copy
entire arrays or subsets of arrays.
 
M

Mattias Sjögren

Tim,
So,
Data is the type of PChar(in Delphi), the same of Char* (in C#). The "^"
is the dereference operator, it takes the value from it.
IntegerValue is a variable of type Integer (in Delphi), the same of Int
(in C#).
SizeOf is very similar in both languages.


Then, which is C#'s equivalent to Delphi's "Move" ?

int IntegerValue = *((int*)Data);

should do it. But there might be a better way to do what you want,
that doesn't require unsafe code.



Mattias
 
O

ozbear

<snip>

Slumming in the .net groups until Borland gets the wheels back on its
news server?

Oz
 
R

Rudy Velthuis

ozbear said:
<snip>

Slumming in the .net groups until Borland gets the wheels back on its
news server?

No, I have been reading this since a few months already. I just didn't
answer a lot yet, since C# is still quite new to me, and others seem to
know more about it than I do.
 

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

Top