469,926 Members | 1,492 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,926 developers. It's quick & easy.

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
Mar 11 '08 #1
6 6916
On Mar 11, 4:30 pm, LPeter <LPe...@discussions.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.ASCII.GetBytes("yourstring")
ASCIIEncoding.ASCII.GetString(yourbytes)
Mar 11 '08 #2
On Mar 11, 5:00 pm, parez <psaw...@gmail.comwrote:
On Mar 11, 4:30 pm, LPeter <LPe...@discussions.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.ASCII.GetBytes("yourstring")
ASCIIEncoding.ASCII.GetString(yourbytes)
Sorry .. Didnt the see the fixed..
Mar 11 '08 #3
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.

Mar 11 '08 #4
On Tue, 11 Mar 2008 13:30:02 -0700, LPeter
<LP****@discussions.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
Mar 11 '08 #5
"LPeter" <LP****@discussions.microsoft.comwrote in message
news:55**********************************@microsof t.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.
Mar 11 '08 #6


"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.CopyTo(0, myStruct.cBuff, 0,
16);" but unfortunately this isn't work...

Mar 12 '08 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

19 posts views Thread by Espen Ruud Schultz | last post: by
6 posts views Thread by Flyingaway | last post: by
7 posts views Thread by Eric | last post: by
1 post views Thread by Eugene Anthony | last post: by
16 posts views Thread by Hugh Janus | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.