473,398 Members | 2,403 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,398 software developers and data experts.

Fixed Byte Array to String

In the example below, I'm trying to convert a fixed byte array to a
string. I get an error about needing to use "fixed" but I have no clue
where to apply it. Help?
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;

namespace TestApp.DIS
{
/// <summary>
/// Specifies the marking for an entity.
/// </summary>
[StructLayout(LayoutKind.Explicit)]
unsafe public struct EntityMarking
{
/// <summary>
/// An 8-bit enumeration (see Section 4 of EBV-DOC).
/// </summary>
[FieldOffset(0)]
public byte characterSet; // 1 byte

/// <summary>
/// A byte array of 11 characters that represents the marking.
/// </summary>
[FieldOffset(1)]
public fixed byte marking[11]; // 11 bytes

public override string ToString()
{
char[] chars = new char[11];
for (int i = 0; i < 11; i++)
{
// ERROR: You cannot use fixed size buffers contained in unfixed
// expressions. Try using the fixed statement.
chars[i] = Convert.ToChar(marking[i]);
}
return (new String(chars));
}
}
}
Jan 4 '07 #1
1 10369
On Thu, 04 Jan 2007 10:31:31 -0600, "O.B." <fu******@bellsouth.net>
wrote:
>In the example below, I'm trying to convert a fixed byte array to a
string. I get an error about needing to use "fixed" but I have no clue
where to apply it. Help?

public override string ToString()
{
char[] chars = new char[11];
for (int i = 0; i < 11; i++)
{
// ERROR: You cannot use fixed size buffers contained in unfixed
// expressions. Try using the fixed statement.
chars[i] = Convert.ToChar(marking[i]);
}
return (new String(chars));
}
}
The text to this error says: "This error occurs if you use the fixed
sized buffer in an expression involving a class that is not itself
fixed. The runtime is free to move the unfixed class around to
optimize memory access, which could lead to errors when using the
fixed sized buffer. To avoid this error, use the fixed keyword on the
statement."

You have to temporarily fix your structure for the duration of the
fixed statement by fixing a pointer to it, and using that pointer to
access your fixed size byte array.
Try this:

public override string ToString()
{
char[] chars = new char[11];

// This is the fixed statement
fixed (EntityMarking* EMptr = &this)
{

for (int i = 0; i < 11; i++)
{
// Access marking[] via the fixed pointer.
chars[i] = Convert.ToChar(EMptr->marking[i]);
}

} // end fixed statement

return (new String(chars));
}

HTH

rossum

Jan 4 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: PHil Coveney | last post by:
Hello, I am making calls into a legacy DLL. One function in this DLL expects as a parameter a struct with several fields, each of which is a 40-char array. The effect of this function is to copy...
5
by: Arthur Mnev | last post by:
This is probably beaten to death subject... Does anyone have a good idea of what penalties are for using Fixed statement in c#. On one side it allows for much greater flexibility with casts and...
8
by: Jami Bradley | last post by:
Hi, I'm looking for an efficient way to do this, because I know it will be heavily used :-) I have a fixed width string and I need to substitute a substring of characters with new values. I...
4
by: Scott Lemen | last post by:
Hi, Some Win APIs expect a structure with a fixed length string. How is it defined in VB .Net 2003? When I try to use the FixedLengthString class I get an "Array bounds cannot appear in type...
4
by: taskswap | last post by:
I'm converting an application that relies heavily on a binary network protocol. Within this protocol are a lot of byte arrays of character data, like: public unsafe struct MsgAddEntry {...
2
by: O.B. | last post by:
Given the following bit of code, the PtrToStructure is not correctly copying the data into the PDU class. After it executes, the pdu.buffer array looks nothing like the data array that I tried to...
6
by: =?Utf-8?B?TWljaGFlbA==?= | last post by:
Hi, I need to create a formatted byte array for SMPP, e.g.: 00 00 00 21 00 00 00 04 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 is the length of the entire...
2
by: O.B. | last post by:
When using Marshal to copy data from a byte array to the structure below, only the first byte of the "other" array is getting copied from the original byte array. What do I need to specify to get...
2
by: ksheerasagar17 | last post by:
Hello All, Scenario: Sending an image through webservice as byte array to an Java webservice. The Problem1: The webservice method image property expects (data type) SByte rather than Byte...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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,...
0
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...
0
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...
0
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,...
0
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...

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.