public static void WriteMpiMatrix(string fileName, string[,] matrix) { using (var writer = new BinaryWriter(File.Open(fileName, FileMode.Create))) { byte[] bytes = { (byte)'M', (byte)'P', (byte)'I', (byte)'M' }; var header = new MpiMatrixHeader { fourCC = BitConverter.ToUInt32(bytes, 0), dataType = (uint)MPI_Datatype.MPI_DOUBLE, height = (uint)matrix.GetLength(0), width = (uint)matrix.GetLength(1), offset = (uint)Marshal.SizeOf(typeof(MpiMatrixHeader)) }; int size = Marshal.SizeOf(typeof(MpiMatrixHeader)); IntPtr ptr = Marshal.AllocHGlobal(size); // Copy the struct to unmanaged memory. Marshal.StructureToPtr(header, ptr, true); var arr = new byte[size]; Marshal.Copy(ptr, arr, 0, size); Marshal.FreeHGlobal(ptr); writer.Write(arr); var provider = new NumberFormatInfo(); provider.NumberDecimalSeparator = "."; for (int i = 0; i < matrix.GetLength(0); i++) { for (int j = 0; j < matrix.GetLength(1); j++) { double value = Convert.ToDouble(matrix[i, j], provider); writer.Write(value); } } writer.Close(); } }
public static void WriteMpiMatrix(string fileName, string[,] matrix) { using (var writer = new BinaryWriter(File.Open(fileName, FileMode.Create))) { byte[] bytes = {(byte) 'M', (byte) 'P', (byte) 'I', (byte) 'M'}; var header = new MpiMatrixHeader { fourCC = BitConverter.ToUInt32(bytes, 0), dataType = (uint) MPI_Datatype.MPI_DOUBLE, height = (uint) matrix.GetLength(0), width = (uint) matrix.GetLength(1), offset = (uint) Marshal.SizeOf(typeof (MpiMatrixHeader)) }; int size = Marshal.SizeOf(typeof (MpiMatrixHeader)); IntPtr ptr = Marshal.AllocHGlobal(size); // Copy the struct to unmanaged memory. Marshal.StructureToPtr(header, ptr, true); var arr = new byte[size]; Marshal.Copy(ptr, arr, 0, size); Marshal.FreeHGlobal(ptr); writer.Write(arr); var provider = new NumberFormatInfo(); provider.NumberDecimalSeparator = "."; for (int i = 0; i < matrix.GetLength(0); i++) for (int j = 0; j < matrix.GetLength(1); j++) { double value = Convert.ToDouble(matrix[i, j], provider); writer.Write(value); } writer.Close(); } }