Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6549931
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T12:09:15+00:00 2026-05-25T12:09:15+00:00

Okay, so I’ve got a game map creater programmed in Java which writes the

  • 0

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);
                }
            }

        }
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T12:09:15+00:00Added an answer on May 25, 2026 at 12:09 pm

    Absolutely correct:

    Java writes as int 32 Big Endian I believe (I could be wrong though).

    Remember, though: a .Net Int32 is Little-Endian 😉

    [Edit] SOLUTION:

    1) Here is Java code that writes 10 integers (Java int’s are 32-bit, Big-endian))

    
        import java.io.*;
    
        public class WriteBinary {
    
          public static void main (String[] args) {
            int[] data = {
              1, 2, 3, 4, 5, 6, 7, 8, 9, 10
            };
    
            String fname = "myfile.bin";
            try
            {
              System.out.println("Opening " + fname + "...");      
              FileOutputStream fos = 
                new FileOutputStream(fname);
              int ibyte;
              for (int i = 0; i < data.length; i++) {
                ibyte = ((data[i] >>> 24) & 0xff); fos.write(ibyte);
                ibyte = ((data[i] >>> 16) & 0xff); fos.write(ibyte);
                ibyte = ((data[i] >>> 8) & 0xff); fos.write(ibyte);
                ibyte = (data[i] & 0xff); fos.write(ibyte);
              }
              fos.close();
              System.out.println("File write complete.");      
            }
            catch (IOException e) {
              System.out.println ("I/O error: " + e.getMessage());
            }
          }
        }
    

    2) Here is the C# code that reads it. You’ll notice the “using System.Net”, in order to get .Net’s equivalent of “ntohl()”:

    
    
    using System;
    using System.IO;
    using System.Net;
    
    namespace ReadBinary
    {
        class Program
        {
            static void Main(string[] args)
            {
                string fname = "myfile.bin";
                try
                {
                    Console.WriteLine("Opening " + fname + "...");
                    BinaryReader br =
                      new BinaryReader(
                            File.Open(fname, FileMode.Open));
                    for (int i = 0; i < (int)(br.BaseStream.Length / 4); i++)
                    {
                        int j =
                            System.Net.IPAddress.NetworkToHostOrder (br.ReadInt32());
                        Console.WriteLine("array[" + i + "]=" + j + "...");
                    }
                    br.Close();
                    Console.WriteLine("Read complete.");
                }
                catch (IOException ex)
                {
                    Console.WriteLine("I/O error" + ex.Message);
                }
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Okay, so I've got some C code to perform a mathematical operation which could,
Okay, so I'm running a small test webserver on my private network. I've got
Okay, I've seen but haven't programmed in C# before. You can assume I'm competent
Okay, so I'm doing my first foray into using the ADO.NET Entity Framework. My
Okay, so I'm working on a project where I use a Java program to
Okay. So I've got a a little jQuery gallery scroller I wrote to work
Okay, I'm reading about Linux kernel development and there are some code snippets using
Okay I got some good advice for Mobile Detection but still having an issue
Okay so for this page: http://www.facebook.com/ESPN?sk=app_224097940938597 Which appears on the left hand bar between
Okay, Ive got a cog (toothed gear) that I have drawn in opengl. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.