473,782 Members | 2,542 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 7451
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
22739
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 copying large text between an std::string and a char pointer, and vice versa. Is there anyhing in the...
6
4269
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
2338
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 path (ie., path information from root to this node)
7
7603
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 idea of how to go about this they may (hopefully) bring that up...?
1
2533
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
2155
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 the string, then reconvert it to a byte. The slow part is the conversion to and from byte, not the...
2
24059
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
39289
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 help! Is there anyone to explain how I can implement these functions by not using .NET library? ...
4
1857
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
9639
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9479
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10311
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10146
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10080
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
9942
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7492
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
1
4043
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3639
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.