// Collection of code snippets by Arne Vajhøj // (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) using System; using System.IO; using Vajhoej.Record; namespace Simple { [Struct] public class Data { [StructField(N=0,Type=FieldType.INT4)] public int Id; [StructField(N=1,Type=FieldType.INT2)] public int V1; [StructField(N=2,Type=FieldType.INT2)] public int V2; [StructField(N=3,Type=FieldType.FIXSTRNULTERM,Length=8,Encoding="ISO-8859-1")] public string S; public override string ToString() { return string.Format("({0},{1},{2},{3})", Id, V1, V2, S); } } public class Program { public static void Main(string[] args) { byte[] b = { 0x01, 0x00, 0x00, 0x00, // Id = 1 0x01, 0x00, // V1 = 1 0x02, 0x00, // V2 = 2 0x41, 0x42, 0x43, 0x00, 0x00, 0x00, 0x00, 0x00, // S = "ABC" 0x002, 0x00, 0x00, 0x00, // Id = 2 0x02, 0x00, // V1 = 2 0x03, 0x00, // V3 = 3 0x41, 0x42, 0x43, 0x44, 0x45, 0x00, 0x00, 0x00 }; // S = "ABCDE" StructReader sr = new StructReader(b); while(sr.More) { Data o = sr.Read(typeof(Data)); Console.WriteLine(o); } } } }