Weird WriteUint8 behavior

Hello

I want to parse a BinaryStream file and change bytes that way:

var Track As UInt8

D81Image.BytePosition = 400128

Do
  MainWindow.Console.Value = MainWindow.Console.Value  + "Read before: " + D81Image.BytePosition.ToString + EndOfLine
  Track = D81Image.ReadUInt8
  MainWindow.Console.Value = MainWindow.Console.Value  + "Read after: " + D81Image.BytePosition.ToString + EndOfLine

  If Track = 0 Then
    MainWindow.Console.Value = MainWindow.Console.Value  + "Write before: " + D81Image.BytePosition.ToString + EndOfLine
    D81Image.WriteUInt8( &hFF )
    MainWindow.Console.Value = MainWindow.Console.Value  + "Write after: " + D81Image.BytePosition.ToString + EndOfLine

    Exit

  Else
    // Jump over to next sector
    D81Image.BytePosition = D81Image.BytePosition + 255
  End If

Loop Until D81Image.BytePosition >= 409600

The output to the console is this:

Read before: 400128
Read after: 400129
Read before: 400384
Read after: 400385
Write before: 400385
Write after: 404481

In second loop iteration the code Track is 0 and heads to “D81Image.WriteUInt8( &hFF )”. The weird thing: Before writing the BytePosition is at 400385 (correct) but after writing a single byte the BytePosition is set to BytePosition 404481 instead of expected 400386. Either there is a bug or I am missing something trivial.

Any ideas about this?

I see no bug here, you’ve done a good job of adding effective logging.

Does it behave this way when you step through the debugger?

Glad to hear it’s not my bug…

Yes, that’s the output running in debug mode, the output is sent to a textarea item. I compiled it as well but same result.

I never worked with the debugger, have to find out how to step through it.

Xojo for Mac, V2025, R3.1

I tried this, but doesn’t show the same problem:

var f as FolderItem = GetFolderItem("test.jpg")
Var b As BinaryStream = BinaryStream.Open(f, true)

b.Position = 123456
Var p1 As Integer = b.Position
b.Write "Hello World"
Var p2 As Integer = b.Position
System.DebugLog str(p2-p1)+" bytes"

Var p3 As Integer = b.Position
b.WriteUInt8 123
Var p4 As Integer = b.Position
System.DebugLog Str(p4-p3)+" bytes"

Var p5 As Integer = b.Position
b.WriteUInt16 123
Var p6 As Integer = b.Position
System.DebugLog Str(p6-p5)+" bytes"

Var p7 As Integer = b.Position
b.Writeint8 123
Var p8 As Integer = b.Position
System.DebugLog Str(p8-p7)+" bytes"

Break

b.Close

Works fine:

     : 11 bytes
     : 1 bytes
     : 2 bytes
     : 1 bytes