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 3690
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 conversion to be simple
... but even using the Convert or...
|
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...
|
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...
|
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...
|
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...
|
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
unmanaged C++ code, but it only occurred once in...
|
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...
|
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=" ";
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
|
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...
|
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,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
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
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
| |