title: Decoding data read from a .bin file into fields
body: <p>I was originally going to read a file through an array, pin to struct, convert and display. I have been trying to find another solution (I have removed the original details here to cause less confusion). I have a .bin File and I can correctly identify how many records are in the file by using a simple sum and <em>FileInfo</em>.</p>
  
  <p>I've looked at:</p>
  
  <ul>
  <li><a href="http://www.sixscape.com/joomla/sixscape/images/pdf/C%20Sharp%20Training%20-%20Part%204%20-%20Contact%20Manager%20with%20Random%20Access%20File.pdf" rel="nofollow noreferrer">http://www.sixscape.com/joomla/sixscape/images/pdf/C%20Sharp%20Training%20-%20Part%204%20-%20Contact%20Manager%20with%20Random%20Access%20File.pdf</a></li>
  <li><a href="http://www.vbi.org/Items/article.asp?id=16" rel="nofollow noreferrer">http://www.vbi.org/Items/article.asp?id=16</a></li>
  </ul>
  
  <p>Files imported have the same structure and look similar to the following: _(note screenshots show the amount of rows in the file and I have then calculated the rows for DataGridView table using a sum from here I produced <a href="https://stackoverflow.com/q/21404668/2952390">Adding rows to second column ONLY - populating data using a for loop</a>:</p>
  
  <pre><code>long Count = 1;
  FileInfo Fi = new FileInfo(import.FileName);
  long sum = (Fi.Length / 1024) - Count;
  
  for (int i = 0; i &lt; sum; i++)
  {
      DataGridView1.Rows.Add(null, Count++);
      ReadWeldRecs(sum); // Added after question was published &amp; called in ReadWeldRecs
  }
  </code></pre>
  
  <p>First shows a total of 21 rows and the second being 9:</p>
  
  <p><img src="https://i.stack.imgur.com/rIPZH.png" alt="enter image description here">
  <img src="https://i.stack.imgur.com/1XhF7.png" alt="enter image description here"></p>
  
  <hr>
  
  <p>I have a method called <em>DecodeDate</em> and another called <em>ReadWeldRecs</em>, <em>DecodeDate</em> is fed through <em>ReadWeldRecs</em> which is then activated via a button click event. I know what date should be displayed, but when it comes to viewing the result from DecodeDate, it is wrong. <strong>I want to be able to read the date inside the file.</strong> <em>import.FileName</em> is the <strong>filename</strong> (@Kevin) that that been opened using <em>OpenFileDialog</em> and the date is displayed at position 5 in the file.</p>
  
  <h1>My first go:</h1>
  
  <p>The Date is displayed as: <em><strong>22/08/2123</strong></em></p>
  
  <p>But should be: <em><strong>21/10/2008</strong></em></p>
  
  <p>I've thought, may be it's an issue with the location? But I'm sure it's position 5. </p>
  
  <h2><strong>Update:</strong> Turns out I was looking in the wrong location... Duh.</h2>
  
  <pre><code>private DateTime DecodeDate(int Start)
  {
      int Year = Strings.Asc(Strings.Mid(import.FileName, Start, 1)) + 2000;
      int Month = Strings.Asc(Strings.Mid(import.FileName, Start + 1, 1));
      int Day = Strings.Asc(Strings.Mid(import.FileName, Start + 2, 1));
  
      return DateAndTime.DateSerial(Year, Month, Day);
  }
  </code></pre>
  
  <h1>Original:</h1>
  
  <p>This is the <strong>original VB</strong> code which worked fine in the out-dated program: <em>(I looked at this mostly to reconstruct the DecodeDate method in C#...)</em></p>
  
  <pre><code>Public Function DecodeDate(Start As Integer) As Date
      YYear = Asc(Mid$(ImportRecord, Start, 1)) + 2000
      MMonth = Asc(Mid$(ImportRecord, Start + 1, 1))
      DDay = Asc(Mid$(ImportRecord, Start + 2, 1))
      DecodeDate = DateSerial(YYear, MMonth, DDay)
  End Function
  </code></pre>
  
  <p>ImportRecord is defined as the following: (global string)</p>
  
  <pre><code>Open ImportFileName For Random As #1 Len = Len(ImportRecord)
  // ...
  Get #1, Index + 1, ImportRecord
  // ...
  .Date = DecodeDate(5) 
  </code></pre>
  
  <h1>Current:</h1>
  
  <pre><code>private void ReadWeldRecs(long RecordNumber)
  {
      byte[] Rec = new byte[1024];
  
      using (FileStream Fs = new FileStream(import.FileName, FileMode.Open, FileAccess.Read))
      using (BinaryReader Br = new BinaryReader(Fs))
      {
          int Rec_Len;
  
          RecordNumber = 0; // Start with Record 0
          while (true)
          {
              Fs.Seek(RecordNumber * 1024, SeekOrigin.Begin); // Position file to record
              Rec_Len = Br.Read(Rec, 0, 1024); // Read the record here
  
              if (Rec_Len == 0) // If reached end of file, end loop
              {
                  break;
              }
  
              if (Rec[0] != 0) // If there is a record, let's display it
              {
                  Label_Date1.Text = DecodeDate(Rec, 28).ToShortDateString();
              }
  
              RecordNumber++; // Read first record ++
          }
  
          Br.Close();
          Fs.Close();
      }
  }
  </code></pre>
  
  <h2>Plus @Kevin's updated solution :)</h2>
  
  <hr>
  
  <p><strong>However</strong>, also this has resolved a major issue I still another where I am trying to go by the guidelines and template of @Kevin's solution for my other method <em>DecodeString</em>.</p>
  
  <p>In VB:</p>
  
  <pre><code>Public Function DecodeString(Start As Integer, Length As Integer) As String
  Dim Count As Integer
  Dummy = Mid(ImportRecord, Start, Length)
  For Count = 1 To Len(Dummy)
    If (Mid$(Dummy, Count, 1) = Chr$(0)) Or (Mid$(Dummy, Count, 1) = Chr$(255)) Then
      Mid$(Dummy, Count, 1) = Chr$(32)
    End If
  Next
  DecodeString = Trim(Dummy)
  End Function
  </code></pre>
  
  <h2><strong>Again, note I'm looking at using the solution as a template for this</strong></h2>