int ID;
int length;
ID = in.readShort();
length = in.readInt();
🔒 Locked
[java] bits and bytes?? :)
Advertisement
Original Post
im working on a 3DS loader, and I think do something wrong with reading the values.. I can read the first chunk ID, but after that its all downhill .. hehe in the doc: http://www.spacesimulator.net/tut4_3dsloader.html, it says that the chunk ID is stored in a two bytes hexadecimal number, and the chunk length is stored in a 4 bytes number.. shouldn't this work ( "in" is a DataInputStream):
I dont think so - as with most file reading class - it will get in the EOL after an integer. Try making a call to length = in.readInt(); a second time and see if that works.
Quote:
Original post by Drew_Benton
I dont think so - as with most file reading class - it will get in the EOL after an integer. Try making a call to length = in.readInt(); a second time and see if that works.
First, DataInputStream is not a file reading class. Unless the underlying source of the data stream has an EOL (since it's a binary file format, it shouldn't) then I don't think EOL is an issue.
OP: First it would help if you gave an example of what value you were expecting to get (if you know it), and what value you actually got. Anyway, I see two possible issues:
1 - The Java int type is signed, the length value is probably an unsigned value
2 - Little endian vs big endian byte ordering
I would believe that htis would work, but the java api spec for DataInputStream states that it should be used to read files specifically written with DataOutputStream for java types, whereas FileInputStream allows you to read a specific number of bytes. I would use that and simply do:
FileInputStream in = new FileInputStream("file");
byte[] ID = new byte[2];
byte[] size = new byte[4];
try
{
in.read(ID);
in.read(size);
} catch (IOException e) {
//Error on input occured
}
Hope this helps
FileInputStream in = new FileInputStream("file");
byte[] ID = new byte[2];
byte[] size = new byte[4];
try
{
in.read(ID);
in.read(size);
} catch (IOException e) {
//Error on input occured
}
Hope this helps
well, what _is_ happening is that I get wrong values :)
what I want to do is to read two values from a file: first a two byte value, then a four byte value.. how can I do that with java? I thought using DataInputStream and use "readShort" / "readInt" would be a good idea :)
what I want to do is to read two values from a file: first a two byte value, then a four byte value.. how can I do that with java? I thought using DataInputStream and use "readShort" / "readInt" would be a good idea :)
Quote:
Original post by Dobbs
Anyway, I see two possible issues:
1 - The Java int type is signed, the length value is probably an unsigned value
2 - Little endian vs big endian byte ordering
1 - thats true, how can I read and store a unsigned value when java does not suport unsigned data types?
2 - well, that also migh be right:p
:)
Quote:
Original post by sushi-one Quote:
Original post by Dobbs
Anyway, I see two possible issues:
1 - The Java int type is signed, the length value is probably an unsigned value
2 - Little endian vs big endian byte ordering
1 - thats true, how can I read and store a unsigned value when java does not suport unsigned data types?
2 - well, that also migh be right:p
:)
1 - Write a couple functions that twiddle the bits I guess. Java ints are just 32 bit 2's complement, writing a conversion function shouldn't be hard.
2 - Find some better documentation for the 3ds file format that mentions the endianness of the values. Java uses big endian and most x86 programs use little endian.
Basically both cases come down to using read() to read single bytes instead of Java ints and shorts, then using the bitwise operators & | >> <<.
tnx to all of you! :)
here is what the doc says, would this mean that .3ds uses the little endian system? :p
[Edited by - sushi-one on December 27, 2004 7:21:06 AM]
here is what the doc says, would this mean that .3ds uses the little endian system? :p
Quote:
The binary information in the 3ds file is written in a special
kind of way. Namely the lest significant byte comes first in an int.
For example: 4A 5C (2 bytes in hex) would be 5C high byte and 4A low
byte. In a long it is: 4A 5C 3B 8F where 5C4A is the low word and 8F 3B
is the high word..
[Edited by - sushi-one on December 27, 2004 7:21:06 AM]
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement