Moving a string to char* and viceversa.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Tim Conner

    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,





  • Greg Ewing [MVP]

    #2
    Re: Moving a string to char* and viceversa.

    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]
    Accomplish Your Mission with Better IT | IT support for nonprofits. You deserve peace of mind. National and remote outsourced nonprofit IT.


    "Tim Conner" <timconner@hotm ail.com> wrote in message
    news:#qbMtYKhDH A.3764@tk2msftn gp13.phx.gbl...[color=blue]
    >
    >
    > 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[/color]
    the[color=blue]
    > 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,
    >
    >
    >
    >
    >[/color]


    Comment

    • Tim Conner

      #3
      Re: Moving a string to char* and viceversa. which is C#'s equivalent to Delphi's &quot;Move&quot ; ?


      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,




      Comment

      • Rudy Velthuis

        #4
        Re: Moving a string to char* and viceversa. which is C#'s equivalent to Delphi's &quot;Move&quot ; ?

        Tim Conner wrote:
        [color=blue]
        > Then, which is C#'s equivalent to Delphi's "Move" ?[/color]

        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)

        Comment

        • Bret Mulvey

          #5
          Re: Moving a string to char* and viceversa. which is C#'s equivalent to Delphi's &quot;Move&quot ; ?

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

          "Tim Conner" <timconner@hotm ail.com> wrote in message
          news:ORX#6CRhDH A.3764@tk2msftn gp13.phx.gbl...[color=blue]
          >
          > 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[/color]
          performed.[color=blue]
          > 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[/color]
          binary[color=blue]
          > 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[/color]
          IntegeValue[color=blue]
          > 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[/color]
          "^"[color=blue]
          > is the dereference operator, it takes the value from it.
          > IntegerValue is a variable of type Integer (in Delphi), the same of[/color]
          Int[color=blue]
          > (in C#).
          > SizeOf is very similar in both languages.
          >
          >
          > Then, which is C#'s equivalent to Delphi's "Move" ?
          >
          >
          > Thanks in advance,
          >
          >
          >
          >[/color]


          Comment

          • Mattias Sjögren

            #6
            Re: Moving a string to char* and viceversa. which is C#'s equivalent to Delphi's &quot;Move&quot ; ?

            Tim,
            [color=blue]
            >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" ?[/color]

            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

            Please reply only to the newsgroup.

            Comment

            • ozbear

              #7
              Re: Moving a string to char* and viceversa. which is C#'s equivalent to Delphi's &quot;Move&quot ; ?

              On Sat, 27 Sep 2003 21:14:17 +0200, "Rudy Velthuis" <rvelthuis@gmx. de>
              wrote:

              <snip>

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

              Oz

              Comment

              • Rudy Velthuis

                #8
                Re: Moving a string to char* and viceversa. which is C#'s equivalent to Delphi's &quot;Move&quot ; ?

                ozbear wrote:
                [color=blue]
                > On Sat, 27 Sep 2003 21:14:17 +0200, "Rudy Velthuis" <rvelthuis@gmx. de>
                > wrote:
                >
                > <snip>
                >
                > Slumming in the .net groups until Borland gets the wheels back on its
                > news server?[/color]

                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)

                Comment

                Working...