Fixed Byte Array to String

Discussion in 'Microsoft C# .NET' started by O.B., Jan 4, 2007.

  1. O.B.

    O.B. Guest

    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 = Convert.ToChar(marking);
    }
    return (new String(chars));
    }
    }
    }
     
    O.B., Jan 4, 2007
    #1
    1. Advertisements

  2. O.B.

    rossum Guest

    On Thu, 04 Jan 2007 10:31:31 -0600, "O.B." <>
    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 = Convert.ToChar(marking);
    > }
    > 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 = Convert.ToChar(EMptr->marking);
    }

    } // end fixed statement

    return (new String(chars));
    }

    HTH

    rossum
     
    rossum, Jan 4, 2007
    #2
    1. Advertisements

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.
Similar Threads
  1. Cory Autry

    float array to byte array

    Cory Autry, Oct 12, 2003, in forum: Microsoft C# .NET
    Replies:
    1
    Views:
    883
    Austin Ehlers
    Oct 13, 2003
  2. Pablo Gutierrez

    cast from "array of byte" to "array of struct"

    Pablo Gutierrez, Jan 27, 2004, in forum: Microsoft C# .NET
    Replies:
    3
    Views:
    1,059
    Steve Waggoner
    Jan 30, 2004
  3. kevinniu

    Howto convert a byte array to double array.

    kevinniu, Jul 19, 2004, in forum: Microsoft C# .NET
    Replies:
    5
    Views:
    2,770
    Jon Skeet [C# MVP]
    Jul 19, 2004
  4. O.B.

    Converting byte[] to fixed byte[]

    O.B., Oct 27, 2006, in forum: Microsoft C# .NET
    Replies:
    2
    Views:
    277
  5. Replies:
    1
    Views:
    341
    Jon Skeet [C# MVP]
    Dec 17, 2006
  6. Peted

    is a byte array Byte[] pass by reference

    Peted, May 11, 2007, in forum: Microsoft C# .NET
    Replies:
    3
    Views:
    510
    Jon Skeet [C# MVP]
    May 11, 2007
  7. Guest
    Replies:
    4
    Views:
    345
    Guest
    Jun 11, 2007
  8. O.B.
    Replies:
    2
    Views:
    554
Loading...