String to fixed buffer (and vice versa)

L

LPeter

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
 
P

parez

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)
 
P

parez

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.

ASCIIEncoding.ASCII.GetBytes("yourstring")
ASCIIEncoding.ASCII.GetString(yourbytes)

Sorry .. Didnt the see the fixed..
 
M

Marra

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.
 
P

Peter Duniho

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
 
W

Willy Denoyette [MVP]

LPeter said:
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.
 
L

LPeter

Marra said:
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...
 
L

LPeter

Willy Denoyette said:
LPeter said:
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.

Thanks, Willy.

1.
You made a fair a question.
I am working on a small device project with Compact Framework.
I have a native (unmanaged) .dll written in C++.
I have to fill and give some simple unsafe structure to this dll (I can not
bypass this way now).
Of course, I don't use fixed character buffer the other (managed) places in
project.

2.
Your solution seems to good but I wouldn't have thought that this is the
simplest way to copy from string to a fixed buffer. I am little disappointed.
Fortunately, the reverse action is perfect for me.

Thanks again.
 
J

Jon Skeet [C# MVP]

1.
You made a fair a question.
I am working on a small device project with Compact Framework.
I have a native (unmanaged) .dll written in C++.
I have to fill and give some simple unsafe structure to this dll (I can not
bypass this way now).
Of course, I don't use fixed character buffer the other (managed) places in
project.

Does the compact framework not support using a string in the struct
and specifying the size as part of an attribute
(MarshalAsAttribute.SizeConst for example)? I can't say I'm really up
to speed on marshalling - and particularly not in the Compact
Framework - but I'm pretty sure there are easier ways than using a
fixed buffer in the C# code.

It may be worth asking in the Compact Framework newsgroup, as I'm sure
people will have experience of doing this with Win32 calls.

Jon
 
L

LPeter

Peter Duniho said:
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

Thanks your answer and your question too, Pete.

Firstly, I have a native (unmanaged) .dll written in C++.
I can not bypass this way because I have to fill and give some simple unsafe
structure to this DLL.
However, you are right and I don't use fixed character buffer the other
(managed) places in my project.

Secondly, for example, I get that error message, if I use the following:
"myString.CopyTo(0, myStruct.cBuff, 0, 16);"
However I understand this error message and its reason too, but I didn't
find any simple way to this special copy.
Of course, the copy each characters by char to char in a for statement is
work, but I did want to use a more simple way.
Perhaps, it's missing...
 
W

Willy Denoyette [MVP]

LPeter said:
Willy Denoyette said:
LPeter said:
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.

Thanks, Willy.

1.
You made a fair a question.
I am working on a small device project with Compact Framework.
I have a native (unmanaged) .dll written in C++.
I have to fill and give some simple unsafe structure to this dll (I can
not
bypass this way now).
Of course, I don't use fixed character buffer the other (managed) places
in
project.

You don't need to declare your char[] as a "fixed" buffer in a structure
that will be passed to unmanaged code, you can declare a char array like
this:

[MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)].
public char[] cBuff;

the interop marshaler will embed the char array when passing the structure
to unmanaged.

2.
Your solution seems to good but I wouldn't have thought that this is the
simplest way to copy from string to a fixed buffer. I am little
disappointed.

Why the dissapointment? When opting for "fixed" buffers, you are dealing
with pointers, just like you would in native C. But again, you don't have to
use fixed and unsafe.

Willy.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top