6

Simple question. What would an equally functioning conversion look like in C#?

VB6:

Dim rec As String * 200
If rs!cJobNum <> "" Then
    Open PathFintest & Mid(rs!cJobNum, 2, 5) & ".dat" For Random As #1 Len = 200
    s = Val(Mid(rs!cJobNum, 7, 4))
    Get #1, Val(Mid(rs!cJobNum, 7, 4)) + 1, rec
    Close #1
    TestRec = rec
    Fail = FindFailure(TestRec)
End If

This was my attempt in C# (doesn't return similar results):

FileStream tempFile = File.OpenRead(tempPath);
var tempBuf = new byte[200];
var tempOffset = Int32.Parse(StringHelper.Mid(rs.Fields["cJobnum"].Value, 7, 4)) + 1;
tempFile.Seek(tempOffset , SeekOrigin.Begin);
tempFile.Read(tempBuf, 0, 200);
rec.Value = new string(System.Text.Encoding.Default.GetChars(tempBuf));
tempFile.Close();
TestRec = rec.Value;
Fail = (string)FindFailure(ref TestRec);
BluishMicrobe
  • 178
  • 1
  • 10
  • 3
    Please read the help centre documentation around code formatting - if you're formatting a block of code, you indent it rather than using backticks... it looks a *lot* clearer that way. You should also try to explain to people who don't know VB6 what `Get` does... otherwise you're restricting yourself to people who know C# *and* VB6... – Jon Skeet Dec 18 '14 at 18:27
  • 3
    @JohnSaunders: No, .NET uses UTF-8 by default - which will be the same as ASCII, for files that only contain ASCII characters. – Jon Skeet Dec 18 '14 at 18:28
  • This is a old question of mine where I was using `Put` instead of `Get` but it might help you get on the right track: http://stackoverflow.com/questions/8043167/how-to-read-and-write-interoperable-strings-to-a-binary-file-between-net-and-vb – Scott Chamberlain Dec 18 '14 at 18:31
  • @JohnSaunders: In that case, text encodings shouldn't be used at all... using ASCII will lose data. There are lots of things wrong with how this code is trying to read data, but until we know what the VB6 code does, it's hard to say what the *right* .NET code would look like. – Jon Skeet Dec 18 '14 at 18:33
  • Please show the declaration of `rec` in your VB6 code. If it is a custom `Type` then show the definition of the type too. – Scott Chamberlain Dec 18 '14 at 18:37

1 Answers1

5

In VB6, strings are stored as Unicode. In memory, VB6 strings store 4 bytes of overhead, plus 2 bytes per character, so your statement Dim rec As String * 200 actually allocates 4 + 200 * 2 bytes of memory, which is 404 bytes. Since VB6 strings and C# strings are both Unicode, you don't need to change anything here.

The Get command in VB6 retrieves bytes from a file. The format is Get [#]filenumber, [byte position], variableName. This will retrieve however many bytes variableName is, starting at an offset of byte position. The byte position is 1-based in VB6.

So now, to translate your code, it should look similar to this:

int pos = (rs.Fields["cJobnum"].Value).SubString(6, 4);
tempFile.Read(tempBuf, pos - 1, 200);

Notice that SubString is 0-based and Mid is 1-based, so I used 6 instead of 7. Also, the offset in the Read method is 0-based. Get is 1-based in VB6, so we subtract one.

Icemanind
  • 47,519
  • 50
  • 171
  • 296
  • Thanks Bro! That fixed the issue, like I said it was a simple fix that has been driving me nuts. So the real byte offset was actually, "1000 x 200 = 200,000" so yeah you nailed my question directly on the head. – BluishMicrobe Dec 18 '14 at 22:18