473,289 Members | 2,089 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,289 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 10339
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: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
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...
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)...

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.