Using App Binary File (made in XOJO) In VS

Hi,

I want to read a binary file, that my XOJO app creates, in C# app (Windows).
I’m not sure how to get C# to read the binary file correctly. In C# when it reads the first “int32” , it’s this long number when it should only be 1 or 2 (possibly 3) digits. Then the PString and the other other int32 read are incorrect.

Is there something special I need to do when using binary files made from apps built in XOJO in C#?
if so how?

My apps saves the data as follows -
write.Writeint32
write.WritePString
write.Writeint32

Thanks.

First, open the binary file in a hex editor and check that what you wrote is in the file.

Next, make sure that C# is reading an INT32, not an ‘Integer’, which might be 64bits in length - if it reads 64 bits (8 bytes) then you lose the first 4 bytes of the PString

Next, look into littleendian property.
By default, Xojo writes ‘BigEndian’
So 32 would look like 00 00 00 20 as an Int32 at the start of the file.

If it is being READ as LittleEndian, then 00 00 00 20 is a very large number. (536870912?)

Edit: A Google search reveals that C# Binary Readers expect a littleendian file

When you open your BinaryStream in Xojo, set the .littleendian property to TRUE before writing.

If thats not good for other reasons, try this replacement BinaryReader class (from rekotc) which reverses the byte order

class BinaryReader2 : BinaryReader { 
    public BinaryReader2(System.IO.Stream stream)  : base(stream) { }

    public override int ReadInt32()
    {
        var data = base.ReadBytes(4);
        Array.Reverse(data);
        return BitConverter.ToInt32(data, 0);
    }

    public Int16 ReadInt16()
    {
        var data = base.ReadBytes(2);
        Array.Reverse(data);
        return BitConverter.ToInt16(data, 0);
    }

    public Int64 ReadInt64()
    {
        var data = base.ReadBytes(8);
        Array.Reverse(data);
        return BitConverter.ToInt64(data, 0);
    }

    public UInt32 ReadUInt32()
    {
        var data = base.ReadBytes(4);
        Array.Reverse(data);
        return BitConverter.ToUInt32(data, 0);
    }

}

I forgot about the LittleEndian/BigEndian thing…

I see cool. Thanks, I appreciate it ! :sunglasses: