Results 1 to 4 of 4
  1. #1
    prich is offline Member
    Join Date
    Aug 2009
    Posts
    7
    Rep Power
    0

    Default Reading BInary Data

    Hello there!

    I'm having some problems reading binary data from a file. I know that this is the networking part of the forum, but i believe this is very similar from reading binary Data from a network stream and from a file stream...

    So my problem is that i have unsigned binary data in my file. So far no problemen because due to this there is the readUnsignedShort() method on the DataInputStream.

    I write with a c# programm a file like this:
    Java Code:
    using (BinaryWriter bin = new BinaryWriter(File.Open("test.bin",FileMode.Create)))
    				{
    						
    						
    					bin.Write((byte)254);
    					bin.Write((sbyte)-126);
    					bin.Write((ushort)495);
    					bin.Write((short)-1323);
    				}
    and i read it in java like this:
    Java Code:
    	  RandomAccessFile raf = new RandomAccessFile(new File("test.bin"), "r");
    	  
    	  System.out.println(raf.readUnsignedByte());
    	  System.out.println(raf.readByte());
    	  System.out.println( raf.readUnsignedShort());
    	  System.out.println(raf.readShort());
    	  
    	  raf.close();
    my results are very confusing:
    254
    -126
    61185
    -10758

    can somebody help me?

    thx prich

  2. #2
    mrmatt1111's Avatar
    mrmatt1111 is offline Senior Member
    Join Date
    Aug 2009
    Location
    San Jose, CA, USA
    Posts
    320
    Rep Power
    6

    Default

    Could be little vs big endian Endianness - Wikipedia, the free encyclopedia

    You might hand check the data in the file and also check for endianess of C# and java.
    My Hobby Project: LegacyClone

  3. #3
    prich is offline Member
    Join Date
    Aug 2009
    Posts
    7
    Rep Power
    0

    Default

    thanks alot!

    although i work for a longer time with java, i do not know how to change the endianess of the RandomAccessFile or DataInputStream...
    Do i HAVE TO use the ByteBuffer or is there another solution?

    thx prich

  4. #4
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    7

    Default

    Take your file and read it byte by byte, and print each byte, in hex, to the console. Make sure the C# program wrote out what you think it did. That will tell you if you have an "endian" problem. If so, your friend the ByteBuffer can help. Once you get used to it, ByteBuffer is fairly easy to use. Of course, it took me about 10 straight hours to get used to it...

Similar Threads

  1. Replies: 5
    Last Post: 08-21-2009, 11:03 AM
  2. Replies: 0
    Last Post: 04-22-2008, 11:57 AM
  3. Reading a Field in a Binary File
    By janakiram.attuluri in forum Advanced Java
    Replies: 2
    Last Post: 01-09-2008, 10:47 AM
  4. Reading Binary File and Changing data
    By janakiram.attuluri in forum Advanced Java
    Replies: 1
    Last Post: 12-21-2007, 10:10 AM
  5. reading a binary file with a RAF
    By jkurth in forum Advanced Java
    Replies: 2
    Last Post: 12-20-2007, 07:30 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •