473,545 Members | 2,004 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Moving a string to char* and viceversa.



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(sBa ck);
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
7 3716
Tim, do you want to go from string to char[] or char[] to string?

for string to char[] you can use string.ToCharAr ray();
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*******@hotm ail.com> wrote in message
news:#q******** ******@tk2msftn gp13.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(sBa ck);
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

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
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.Co py(), or String.ToCharAr ray(), or
System.Text.Uni codeEncoding.Ge tBytes(), 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
Take a look at Array.Copy. It has different forms to allow you to copy
entire arrays or subsets of arrays.

"Tim Conner" <ti*******@hotm ail.com> wrote in message
news:OR******** ******@tk2msftn gp13.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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
1584
by: Bob Rock | last post by:
Hello, a simple question (if you know the answer) ... how to convert from and array of bytes to a string (and viceversa). I haven't the conversion to be simple ... but even using the Convert or the Encoding class I seem to be having trouble accomplishing it. Bob Rock
4
2584
by: Dave | last post by:
Hello all, The scheme shown below to move a text file's contents into a std::string works with one exception: it drops the carriage return and line feed characters. How may I, in a Standard-compliant way, read in a text file's contents and keep the carriage returns and line feeds? Thanks, Dave
0
1363
by: Shibu | last post by:
Hi, I have a situation where I need to convert business objects to a flat table. The reverse is also required. I am using c# and Oracle ODP. I am looking for an easier method to do the below steps. Steps I follows for populating Business Objects is as follows (1) Get a list of records containing various tables joined together from DB...
3
3159
by: Stefania Scott | last post by:
How do I resolve the problem of passing a string that has quotes within in a SQL statement? Sometimes the string contains a single quote (') and some others it contains the double quote (")? Any clue... Thank you, Stefania
4
1585
by: mt | last post by:
Hi, W H A T I am considering moving my windows app written in Visual C++ 6.0 to C# .NET. Q U E S T I O N I was wondering if application speed will be a problem in .NET when compared to a VC++6? Am I going to take a big performance hit?
3
18176
by: Mike | last post by:
Hi, Does anyone know of reliable programs that convert C# to Java and viceversa? Thanks Mike
1
1276
by: Lance Orner | last post by:
I wrote this letter to a colleague, and I thought I'd share it here: --- We had a problem with pointers being passed between managed C++ and unmanaged C++ code, but it only occurred once in every 2000-3000 calls. There were some String* objects in the managed code, which were marshalled into IntPtr* objects. This was cast into a const...
2
5018
by: Alejandro Aleman | last post by:
Hello! i know this may be a newbie question, but i need to convert a string from System::String^ to char*, in the msdn page tells how, but i need to set to /clr:oldSyntax and i dont want it because in further editions of .net this will be deprecated or so on.. . so, please, anybody can tellme how to convert from System::String::^ to...
2
1866
by: ziibrs | last post by:
Is it true that it is not allowed to overwrite statically initialized char * ? For example: #include <stdio.h> #include <string.h> int main(void) { char *dest=" ";
0
7484
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7415
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7675
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7440
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
4963
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3470
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3451
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1902
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1030
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.