473,769 Members | 2,003 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.Collecti ons.Generic;
using System.Text;
using System.Runtime. InteropServices ;

namespace TestApp.DIS
{
/// <summary>
/// Specifies the marking for an entity.
/// </summary>
[StructLayout(La youtKind.Explic it)]
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 10431
On Thu, 04 Jan 2007 10:31:31 -0600, "O.B." <fu******@bells outh.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
464
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 characters into these char array fields. I am calling this function with (roughly) the following: public class Foo { public byte A;
5
8324
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 array manipulations (i'm leaving access to legacy code out of the scope of this message) on the other hand fixed statement does consume resources to execute, not to mention if "everything" is fixed, then dynamic object reallocation becomes...
8
5420
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 can do this with 2 substring calls, but it will need to rebuild the string just to write a few characters. Here is the simple, but inefficient, version: string s = "0123456789";
4
23823
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 specifiers" error. Thank you, Scott
4
2195
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 { public byte MsgType; public uint Tag; public fixed byte ID; public fixed byte Val1;
2
3850
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 copy from. Help? public unsafe struct PDU { public const int MAX_PDU_SIZE = 1400;
6
3569
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 message in octets, however the value is only in the 4 octet, is there someway I can format this into fixed width, a comparison would be
2
7207
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 Marshal.PtrToStructure to copy the all the data into the "other" array? unsafe public struct DeadReckoning {
2
9789
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 array. Thus i'm converting a byte array to sbyte array and sending through web service. The converted SByte contains negative numbers wihch are resulting in an error "java.lang.ArrayIndexOutOfBoundsException: -106" Byte => SByte...error...
0
9589
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
9423
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
10211
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
10045
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
9994
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
9863
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...
0
8870
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5298
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5447
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.