Skip to main content
GameDev.net gamedev.net
🔒 Locked

[java] bits and bytes?? :)

Started by sushi-one Dec 26, 2004 at 8:33 PM 10 replies 1.9k views
Original Post
sushi-one
sushi-one
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):

int ID;
int length;

ID = in.readShort();
length = in.readInt();



Hans-Petter Harveg
Drew_Benton
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.
sushi-one
sushi-one
EOL? but i am not reading a text file and wouldnt that mean I was reading 4 extra bytes? (passed the current chunk) :)
Hans-Petter Harveg
Morbo
Morbo
Well without having any deatails about what _is_ happening ('all downhill' is rather vague), I'd say you might have to worry about what endian the 3DS ints are stored as, vs. what Java is expecting. I don't remember off-hand whether 3DS stores little- or bit-endian.
Dobbs
Dobbs
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
Cloxs
Cloxs
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
sushi-one
sushi-one
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 :)

Hans-Petter Harveg
sushi-one
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

:)
Hans-Petter Harveg
Dobbs
Dobbs
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 & | >> <<.
sushi-one
sushi-one
tnx to all of you! :)

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]
Hans-Petter Harveg
Dobbs
Dobbs
Yes, least significant byte (lsb) first is the same as little endian.
phaelax
phaelax
i believe 3ds files are written in little endian and java reads big endian, i think. im not sure.
oh, guess that was right. so just right a function to do a bit of bitwise operations to switch the order of bytes after you read them in.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.