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, 7 3567
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,
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,
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)
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,
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.
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
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) This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
by: Mike |
last post by:
Hi,
Does anyone know of reliable programs that convert C# to Java and viceversa?
Thanks
Mike
|
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...
|
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...
|
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...
|
by: concettolabs |
last post by:
In today's business world, businesses are increasingly turning to PowerApps to develop custom business applications. PowerApps is a powerful tool...
|
by: better678 |
last post by:
Question:
Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct?
Answer:
Java is an object-oriented...
|
by: teenabhardwaj |
last post by:
How would one discover a valid source for learning news, comfort, and help for engineering designs? Covering through piles of books takes a lot of...
|
by: CD Tom |
last post by:
This happens in runtime 2013 and 2016. When a report is run and then closed a toolbar shows up and the only way to get it to go away is to right...
|
by: CD Tom |
last post by:
This only shows up in access runtime. When a user select a report from my report menu when they close the report they get a menu I've called Add-ins...
|
by: Naresh1 |
last post by:
What is WebLogic Admin Training?
WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
|
by: Matthew3360 |
last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
|
by: AndyPSV |
last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
|
by: WisdomUfot |
last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...
| |