Guest User

Untitled

a guest
Mar 19th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.20 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using System.Runtime.Serialization.Formatters.Binary;
  10. using System.IO;
  11.  
  12. using Microsoft.VisualStudio.TestTools.UnitTesting;
  13. using System.Runtime.InteropServices;
  14.  
  15. /**
  16. * CharSet defined as Ansi
  17. * Use CharSet.Unicode for scandinavian chars,
  18. * Unicode might fuck up byte sizes
  19. */
  20. [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
  21. public struct Struct1
  22. {
  23. [MarshalAs(UnmanagedType.ByValArray, SizeConst = 4)]
  24. public char[] voucher_type;
  25.  
  26. [MarshalAs(UnmanagedType.ByValArray, SizeConst = 2)]
  27. public char[] trans_type;
  28.  
  29. [MarshalAs(UnmanagedType.ByValArray, SizeConst = 3)]
  30. public char[] currency;
  31.  
  32. [MarshalAs(UnmanagedType.ByValArray, SizeConst = 4)]
  33. public char[] cur_amount;
  34.  
  35. [MarshalAs(UnmanagedType.ByValArray, SizeConst = 4)]
  36. public char[] amount;
  37.  
  38. [MarshalAs(UnmanagedType.ByValArray, SizeConst = 3)]
  39. public char[] apar_type;
  40.  
  41. public void Write(BinaryWriter writer, byte[] newline)
  42. {
  43. var sz = Marshal.SizeOf(this);
  44. IntPtr pStruct = Marshal.AllocHGlobal(sz);
  45.  
  46. Marshal.StructureToPtr(this, pStruct, false);
  47. var buffer = Array.CreateInstance(typeof(byte), sz) as byte[];
  48. Marshal.Copy(pStruct, buffer, 0, buffer.Length);
  49.  
  50. writer.Write(buffer);
  51. writer.Write(newline);
  52.  
  53. Marshal.Release(pStruct);
  54. }
  55.  
  56. public static Struct1 Read(BinaryReader reader)
  57. {
  58. var sz = Marshal.SizeOf(typeof(Struct1));
  59. var buffer = reader.ReadBytes(sz);
  60.  
  61. GCHandle handle = GCHandle.Alloc(buffer, GCHandleType.Pinned);
  62.  
  63. Struct1 result = (Struct1)
  64. Marshal.PtrToStructure(
  65. handle.AddrOfPinnedObject(),
  66. typeof(Struct1));
  67.  
  68. handle.Free();
  69. return result;
  70. }
  71. }
  72.  
  73.  
  74. class Foo {
  75.  
  76. public static void simple(string fileName) {
  77.  
  78. byte[] newline = Encoding.ASCII.GetBytes(Environment.NewLine);
  79.  
  80. List<Struct1> expected = new List<Struct1>();
  81.  
  82. expected.Add(new Struct1() {
  83. voucher_type = "0142".ToCharArray(),
  84. trans_type = "AR".ToCharArray(),
  85. currency = "NOK".ToCharArray(),
  86. cur_amount = "9999".ToCharArray(),
  87. amount = "9999".ToCharArray(),
  88. apar_type = "001".ToCharArray(),
  89. });
  90.  
  91. expected.Add(new Struct1() {
  92. voucher_type = "0142".ToCharArray(),
  93. trans_type = "GL".ToCharArray(),
  94. currency = "NOK".ToCharArray(),
  95. cur_amount = "9999".ToCharArray(),
  96. amount = "9999".ToCharArray(),
  97. apar_type = "001".ToCharArray(),
  98. });
  99.  
  100.  
  101. using (FileStream fs = File.Open(fileName, FileMode.Create, FileAccess.Write, FileShare.None))
  102. {
  103. var writer = new BinaryWriter(fs);
  104. foreach (var item in expected)
  105. {
  106. item.Write(writer, newline);
  107. }
  108. }
  109.  
  110. }
  111.  
  112. static void Main(string[] args)
  113. {
  114. simple("tralala.txt");
  115. }
  116. }
  117.  
  118. /* PRODUCES
  119. 0142ARNOK99999999001
  120. 0142GLNOK99999999001
  121.  
  122. */
Add Comment
Please, Sign In to add comment