> Hi,
>
[quoted text clipped - 17 lines]
>
> LPeter
ASCIIEncoding.ASCII.GetBytes("yourstring")
ASCIIEncoding.ASCII.GetString(yourbytes)
parez - 11 Mar 2008 22:02 GMT
> > Hi,
>
[quoted text clipped - 20 lines]
> ASCIIEncoding.ASCII.GetBytes("yourstring")
> ASCIIEncoding.ASCII.GetString(yourbytes)
Sorry .. Didnt the see the fixed..
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.
LPeter - 12 Mar 2008 10:50 GMT
> 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...
> I would copy the characters of a string variable to a fixed character
> buffer
[quoted text clipped - 11 lines]
> "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
LPeter - 12 Mar 2008 11:33 GMT
> > I would copy the characters of a string variable to a fixed character
> > buffer
[quoted text clipped - 30 lines]
>
> 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...
> Hi,
>
[quoted text clipped - 17 lines]
>
> 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.
LPeter - 12 Mar 2008 11:12 GMT
> > Hi,
> >
[quoted text clipped - 45 lines]
> 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.
Jon Skeet [C# MVP] - 12 Mar 2008 11:21 GMT
<snip>
> 1.
> You made a fair a question.
[quoted text clipped - 4 lines]
> 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
Willy Denoyette [MVP] - 12 Mar 2008 13:04 GMT
>> > Hi,
>> >
[quoted text clipped - 58 lines]
> 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.