473,326 Members | 2,438 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

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 7363
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

19
by: Espen Ruud Schultz | last post by:
Lets say I have a char pointer and an std::string. Is it possible to get a pointer to the std::string's "content" so that the char pointer can point to the same text? And vice versa; can I give...
6
by: Flyingaway | last post by:
I am just wondering what is the most elegant and efficient way to do such type conversion. Thanks, Flyingway
1
by: Srini | last post by:
Hi, I am working on a project and a portion of which involves receiving xml files on internet, extract values to build a string and pass that string to legacy system. I am planning on using...
7
by: Eric | last post by:
Hi All, I need to XOR two same-length Strings against each other. I'm assuming that, in order to do so, I'll need to convert each String to a BitArray. Thus, my question is this: is there an...
1
by: Eugene Anthony | last post by:
Private Function BStr2UStr(BStr) 'Byte string to Unicode string conversion Dim lngLoop BStr2UStr = "" For lngLoop = 1 to LenB(BStr) BStr2UStr = BStr2UStr & Chr(AscB(MidB(BStr,lngLoop,1))) Next...
16
by: Hugh Janus | last post by:
Hi all, I am using the below functions in order to convert strings to bytes and vice versa. I totally ans shamefully stole these functions from this group btw! Anyway, they work great but as...
2
yabansu
by: yabansu | last post by:
Hi all, This is my first message! Using C++ standard libraries, I want to convert a string to a byte array and a byte array to the string. How can I do that? I did it in C# .NET as the...
6
yabansu
by: yabansu | last post by:
Hi all, I think most of you probably know the two .NET framework functions, namely Encoding.GetBytes(string) and Encoding.GetString(byte), to convert string into byte array and vice versa. Now,...
4
by: Jack | last post by:
Hi, I do a webrequest and it returns some text data in a stream. I want to put this text data into a string. I've got it working just fine, but I have to put the text data into into a...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.