By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,864 Members | 1,324 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,864 IT Pros & Developers. It's quick & easy.

Moving a string to char* and viceversa.

P: n/a


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,

Nov 15 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
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.

--
Greg Ewing [MVP]
http://www.citidc.com

"Tim Conner" <ti*******@hotmail.com> wrote in message
news:#q**************@tk2msftngp13.phx.gbl...


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,


Nov 15 '05 #2

P: n/a

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,


Nov 15 '05 #3

P: n/a
Tim Conner wrote:
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.
--
Rudy Velthuis

"The truth is more important than the facts."
- Frank Lloyd Wright (1868-1959)
Nov 15 '05 #4

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

"Tim Conner" <ti*******@hotmail.com> wrote in message
news:OR**************@tk2msftngp13.phx.gbl...

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,

Nov 15 '05 #5

P: n/a
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

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/
Please reply only to the newsgroup.
Nov 15 '05 #6

P: n/a
On Sat, 27 Sep 2003 21:14:17 +0200, "Rudy Velthuis" <rv*******@gmx.de>
wrote:

<snip>

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

Oz
Nov 15 '05 #7

P: n/a
ozbear wrote:
On Sat, 27 Sep 2003 21:14:17 +0200, "Rudy Velthuis" <rv*******@gmx.de>
wrote:

<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.
--
Rudy Velthuis

"Glory is fleeting, but obscurity is forever."
-- Napoleon Bonaparte (1769-1821)
Nov 15 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.