0

I have a list of struct, with each struct having two short values and a byte value. I would like to write this to a HEX file just containing the raw data. This HEX value will get loaded onto a microcontroller.

Can anyone point me in the right direction on how to dump these values into a file.?

3
  • Can you provide details, such as how you want a short or a byte value formatted? Any spaces between the hex values? What kind of list of what kind of structs? Commented Nov 2, 2010 at 20:53
  • I don't care how the short is formatted, so long as I know what the format is Commented Nov 2, 2010 at 21:27
  • Is the list of structs the whole content of the file? Commented Nov 2, 2010 at 21:56

3 Answers 3

1

Let's say your struct is

struct Package {
    public short First { get; private set; }
    public short Second { get; private set; }
    public byte Third { get; private set; }
    public Package(short first, short second, byte third) : this() {
        this.First = first;
        this.Second = second; 
        this.Third = third;
    }
}

Then:

void WritePackageOnWriter(Package package, BinaryWriter binaryWriter) {
    binaryWriter.Write(package.First);
    binaryWriter.Write(package.Second);
    binaryWriter.Write(package.Third);
}

So that

// binaryWriter is BinaryWriter
// packages is IEnumerable<Package>
foreach(var package in packages) {
    WritePackageOnWriter(package, binaryWriter);
}

If necessary, you can reconstitute via

Package ReadPackageFromReader(BinaryReader binaryReader) {
    short first = binaryReader.ReadInt16();
    short second = binaryReader.ReadInt16();
    byte third = binaryReader.ReadByte();
    return new Package(first, second, third);
}
Sign up to request clarification or add additional context in comments.

1 Comment

Roughly what I was writing but expressed better.
0

I assume that by a hex file you actually mean a binary file, not a text file with hex codes.

The structure will have padding in it, so you can't just dump the list straight into a file.

Just get the values from each struct and write to the file using a BinaryWriter:

using (FileStream file = File.OpenWrite("FileName.ext")) {
  using (BinaryWriter writer = new BinaryWriter(file)) {
    foreach (Data data in theList) {
      writer.Write(data.FirstShort);
      writer.Write(data.SecondShort);
      writer.Write(data.TheByte);
    }
  }
}

Comments

0

Look at this site: http://www.developerfusion.com/article/84519/mastering-structs-in-c/

I think you will want more control over your data types especially with Serialize/Deserializing your data. This will allow you to control EXACTLY how you marshall your datatypes.

Taken Directly from Mastering Structs in C#

Struct Layout

[StructLayout(LayoutKind.Sequential,
Pack = 1, CharSet = CharSet.Unicode)]
public struct DISPLAY_DEVICE
{
    public int cb;
    [MarshalAs(
        UnmanagedType.ByValArray,
        SizeConst=32)]
        public char[] DeviceName;
    [MarshalAs(
        UnmanagedType.ByValArray,
        SizeConst=128)]
        public char[] DeviceString;
    public int StateFlags;
    [MarshalAs(
        UnmanagedType.ByValArray,
        SizeConst = 128)]
        public char[] DeviceID;
    [MarshalAs(
        UnmanagedType.ByValArray,
        SizeConst = 128)]
        public char[] DeviceKey;
}

Serialization

public static byte[]
    RawSerialize(object anything)
{
    int rawsize =
        Marshal.SizeOf(anything);
    IntPtr buffer =
        Marshal.AllocHGlobal(rawsize);
    Marshal.StructureToPtr(anything,
        buffer, false);
    byte[] rawdata = new byte[rawsize];
    Marshal.Copy(buffer, rawdata,
        0, rawsize);
    Marshal.FreeHGlobal(buffer);
    return rawdata;
}

Side note:

For basic serialization I would not try to roll my own I would just use built in .NET serialization: http://msdn.microsoft.com/en-us/library/4abbf6k0.aspx

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.