Okay, so I've got a game map creater programmed in Java which writes the map out to file using ObjectOutputStream.writeInt()
Now I'm converting the game engine to C# XNA and I'm trying to load the map. I'm getting numerical errors though, so I'm wondering if anyone knows what I'm doing wrong?
Java writes as int 32 Big Endian I believe (I could be wrong though).
Here is the code I'm using to read the height and width of the map in C#.
Edit: br is BinaryReader.
width = (int)IPAddress.NetworkToHostOrder(BitConverter.ToInt32(br.ReadBytes(sizeof(int)), 0));
height = (int)IPAddress.NetworkToHostOrder(BitConverter.ToInt32(br.ReadBytes(sizeof(int)), 0));
Can anyone please tell me what I'm doing wrong? Or how to read the bytes from ObjectOutputStream.writeInt() properly in C#?
Edit: 2nd try failed. here is the current code:
public byte[] ReadBigEndianBytes(int count, BinaryReader br)
{
byte[] bytes = new byte[count];
for (int i = count - 1; i >= 0; i--)
bytes[i] = br.ReadByte();
return bytes;
}
public void loadFile(int level)
{
FileStream fs = new FileStream("map" + level + ".lv", FileMode.Open, FileAccess.Read);
BinaryReader br = new BinaryReader(fs, System.Text.Encoding.BigEndianUnicode);
width = BitConverter.ToInt32(ReadBigEndianBytes(4, br), 0);
height = BitConverter.ToInt32(ReadBigEndianBytes(4, br), 0);
tile = new int[width, height];
for (int x = 0; x < width; x++)
{
for (int y = 0; y < height; y++)
{
tile[x, y] = BitConverter.ToInt32(ReadBigEndianBytes(4, br), 0);
}
}
}
}