PC Review


Reply
Thread Tools Rate Thread

Fixed Byte Array to String

 
 
O.B.
Guest
Posts: n/a
 
      4th Jan 2007
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));
}
}
}
 
Reply With Quote
 
 
 
 
rossum
Guest
Posts: n/a
 
      4th Jan 2007
On Thu, 04 Jan 2007 10:31:31 -0600, "O.B." <(E-Mail Removed)>
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

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to convert byte() to string, and from string back to byte() moondaddy Microsoft VB .NET 4 15th Aug 2005 03:25 PM
How do I convert a ASCII Byte Array, to another Byte Array Russell Mangel Microsoft Dot NET Framework 2 2nd Feb 2005 07:01 PM
Re: Byte Array to Printable String to Byte Array Jon Skeet [C# MVP] Microsoft Dot NET 0 4th Aug 2004 01:53 PM
Is a vb6 byte array the same as a vb.net byte array? Metallicraft Microsoft VB .NET 1 20th Dec 2003 12:13 AM
string >> byte array >> string representation >> byte array >> string !! David Bargna Microsoft VB .NET 4 6th Oct 2003 04:02 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 02:52 AM.