473,548 Members | 2,636 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 7414
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)
Mar 11 '08 #2
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..
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****@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
Mar 11 '08 #5
"LPeter" <LP****@discuss ions.microsoft. comwrote in message
news:55******** *************** ***********@mic rosoft.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.CopyT o(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
22709
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 the std::string a pointer and a length and then give the std::string control over the pointer and its content? I'm basically trying to avoid...
6
4255
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
2328
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 XMLReader to read passed xml file and extract values that need to be mapped to string buffer. However, in this method I am not able to get the full...
7
7580
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 easy way to convert a String to a BitArray (and back again)? I explained the ultimate goal (XORing two Strings) so that, if anyone has a better...
1
2497
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 End Function Private Function UStr2Bstr(UStr)
16
2133
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 sooooo slow. Anyone know how I can speed this functions up? I basically need to convert a byte to string, perform a function on each 'section' of...
2
23957
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 following: string msg = "Hello!"; byte buffer = new byte;
6
39250
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, I want to do the same thing in pure(unmanaged) C++. I searched the Internet but could not find any satisfactory solutions. I am really in need of...
4
1849
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 fixed-size buffer BEFORE I put it into a string (ConstBufferByteSize=1000000). This fixed size buffer wastes space. Is
0
7512
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7951
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7466
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7803
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6036
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5362
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5082
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
1
1051
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
751
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.