String to fixed buffer (and vice versa)

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?Utf-8?B?TFBldGVy?=

    String to fixed buffer (and vice versa)


    Hi,

    I would copy the characters of a string variable to a fixed character buffer
    in a struct (and vice versa) in C#.

    public struct S
    {
    ...
    public fixed char cBuff[16];
    ...
    }


    I tried to do this many way, but I often get the following compiler error:
    "error CS1666: You cannot use fixed size buffers contained in unfixed
    expressions"

    What is the simplest way to do this?
    Thanks for any help.

    LPeter


  • parez

    #2
    Re: String to fixed buffer (and vice versa)

    On Mar 11, 4:30 pm, LPeter <LPe...@discuss ions.microsoft. comwrote:
    Hi,
    >
    I would copy the characters of a string variable to a fixed character buffer
    in a struct (and vice versa) in C#.
    >
    public struct S
    {
    ...
    public fixed char cBuff[16];
    ...
    >
    }
    >
    I tried to do this many way, but I often get the following compiler error:
    "error CS1666: You cannot use fixed size buffers contained in unfixed
    expressions"
    >
    What is the simplest way to do this?
    Thanks for any help.
    >
    LPeter
    ASCIIEncoding.A SCII.GetBytes(" yourstring")
    ASCIIEncoding.A SCII.GetString( yourbytes)

    Comment

    • parez

      #3
      Re: String to fixed buffer (and vice versa)

      On Mar 11, 5:00 pm, parez <psaw...@gmail. comwrote:
      On Mar 11, 4:30 pm, LPeter <LPe...@discuss ions.microsoft. comwrote:
      >
      >
      >
      Hi,
      >
      I would copy the characters of a string variable to a fixed character buffer
      in a struct (and vice versa) in C#.
      >
      public struct S
      {
      ...
      public fixed char cBuff[16];
      ...
      >
      }
      >
      I tried to do this many way, but I often get the following compiler error:
      "error CS1666: You cannot use fixed size buffers contained in unfixed
      expressions"
      >
      What is the simplest way to do this?
      Thanks for any help.
      >
      LPeter
      >
      ASCIIEncoding.A SCII.GetBytes(" yourstring")
      ASCIIEncoding.A SCII.GetString( yourbytes)
      Sorry .. Didnt the see the fixed..

      Comment

      • Marra

        #4
        Re: String to fixed buffer (and vice versa)

        I quite often copy strings to unsigned fixed char arrays.
        Clearly you need to make sure the string fits exactly or pad out the
        array or string to teh correct size.

        Comment

        • Peter Duniho

          #5
          Re: String to fixed buffer (and vice versa)

          On Tue, 11 Mar 2008 13:30:02 -0700, LPeter
          <LPeter@discuss ions.microsoft. comwrote:
          I would copy the characters of a string variable to a fixed character
          buffer
          in a struct (and vice versa) in C#.
          >
          public struct S
          {
          ...
          public fixed char cBuff[16];
          ...
          }
          >
          >
          I tried to do this many way, but I often get the following compiler
          error:
          "error CS1666: You cannot use fixed size buffers contained in unfixed
          expressions"
          "Often"? In what context? Can you post an example of when you get that
          error, and a clearer description of why you don't understand the error?
          What is the simplest way to do this?
          Well, the example you posted is fine, as far as it goes. So the real
          question is, how are you trying to use a struct declared in that way, and
          why isn't _that_ working?

          My personal opinion is that you should think very carefully before using
          "fixed". It's only needed in specialized situations, and like many
          specialized expressions, is over-used. But if we take as granted that you
          need to use a fixed char[] in a struct, there should be a way to explain
          how to do that. But without more information about what you're trying to
          do and why it's not working, it's not possible to reliably answer your
          question.

          Pete

          Comment

          • Willy Denoyette [MVP]

            #6
            Re: String to fixed buffer (and vice versa)

            "LPeter" <LPeter@discuss ions.microsoft. comwrote in message
            news:55443BCB-190D-4EA1-8339-A13C16B679F7@mi crosoft.com...
            >
            Hi,
            >
            I would copy the characters of a string variable to a fixed character
            buffer
            in a struct (and vice versa) in C#.
            >
            public struct S
            {
            ...
            public fixed char cBuff[16];
            ...
            }
            >
            >
            I tried to do this many way, but I often get the following compiler error:
            "error CS1666: You cannot use fixed size buffers contained in unfixed
            expressions"
            >
            What is the simplest way to do this?
            Thanks for any help.
            >
            LPeter
            >
            >

            Here is one possible way to do this:

            const int len = 16;
            internal struct SomeStruct
            {
            internal unsafe fixed char ca[len];
            }
            ....
            SomeStruct someStruct = new SomeStruct ();
            string s = "Hello";
            char *pca = someStruct.ba;
            // make sure your string fits in the fixed array!!!!!!
            //....
            foreach(char ch in s)
            {
            *pca++ = ch;
            }

            // reverse action
            pca = c.ba;
            string s = new String(pca, 0, len);


            But the real question for you to answer is - why do I need fixed buffer?,
            there is likely no good answer to it ....

            Willy.


            Comment

            • =?Utf-8?B?TFBldGVy?=

              #7
              Re: String to fixed buffer (and vice versa)



              "Marra" wrote:
              I quite often copy strings to unsigned fixed char arrays.
              Clearly you need to make sure the string fits exactly or pad out the
              array or string to teh correct size.
              >
              O.K. but how do do the copying ?

              I would use a way which is similar to "myString.CopyT o(0, myStruct.cBuff, 0,
              16);" but unfortunately this isn't work...

              Comment

              Working...