wood burning stoves 2.0*
The moose likes I/O and Streams and the fly likes writing 4 byte little-endian long integers Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "writing 4 byte little-endian long integers" Watch "writing 4 byte little-endian long integers" New topic
Author

writing 4 byte little-endian long integers

Ken Blair
Ranch Hand

Joined: Jul 15, 2003
Posts: 1078
I have a file that is a series of records and subrecords. Each record has a 4 byte string (4 characters, 8 bits per char obviously) followed by a 12 byte long and each subrecord has a 4 byte string and 4 byte long, they use little-endian. I can read & write the strings fine and I can read in the 4/12 byte longs and convert them using a ByteBuffer (using the ByteOrder and getLong) but writing them is causing me issues. I can't figure out how to turn get them back into little-endian and guarantee that they're 12 and 4 bytes where appropriate. Yes, I realize that a long integer shouldn't be 12 bytes but I have no control over the file format. I can write a 4 byte integer using DataOutputStream.writeInt but unfortunately that uses big-endian. I've also figured out how to, instead of using a long, use a BigInteger and then use the BigInteger.toByteArray but I'm not quite sure how I go about converting that to little-endian and adding in the null bytes so that it's 12 or 4 as necessary.
Any help is appreciated, this has been more difficult than I would have expected.
Ken Blair
Ranch Hand

Joined: Jul 15, 2003
Posts: 1078
Alright I got it working by throwing out the ByteBuffer and just reading backwards into a byte array (effective converting to big-endian) and then making a BigInteger with it. I can write as well by using toByteArray, creating a seperate new array that's the size I need, and then adding the other byte array in reverse order and plopping in null values as necessary.
But... first of all what's a better way and second I also have one 4 byte float to deal with too.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
I think your "4 byte long" is what we'd call an int. For the int and the float, ByteBuffer's getInt()/getFloat() and putInt()/putFloat() seem most convenient. They should take the ByteOrder into account for you. For the 12-byte integer (which we probably should not call a long, since it isn't) we don't have a preexisting method to rely on since this isn't a standard JAva primitive. But it's not tough to reverse the order as you transfer bytes out of the buffer:

[ January 18, 2004: Message edited by: Jim Yingst ]

"I'm not back." - Bill Harding, Twister
Ken Blair
Ranch Hand

Joined: Jul 15, 2003
Posts: 1078
Okay so I can use that for the 4 byte integer and the float, however, how do I convert the 12 byte integer to an integer and then back again. I mean, I don't know how I would turn that integer back into a 12 byte integer. I suppose I could pretty safely just use the putInt and then throw in null values to make it 12 bytes?
It's not like I'm ever going to need more than 4 bytes, I don't even know why the hell it's 12 bytes.
Ken Blair
Ranch Hand

Joined: Jul 15, 2003
Posts: 1078
ahem... I ended up with this... for reading anyway...

I think I can write in basically the same way. I'll just use putLong and then add null values to make up the extra 4 bytes myself.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Okay so I can use that for the 4 byte integer and the float, however, how do I convert the 12 byte integer to an integer and then back again.
Reading the 12 bytes and converting to a BigInteger is what I showed in the code. To write it is fairly similar:

I suppose I could pretty safely just use the putInt and then throw in null values to make it 12 bytes?
Yes, if you're sure that the extra 8 bytes are never really needed. If that's the case, you don't really need a BigInteger either, and could read with a getInt() - followed by two more getInt() calls which you simply ignore, since they should contain nothing but zeros.
It's not like I'm ever going to need more than 4 bytes, I don't even know why the hell it's 12 bytes.
[ January 18, 2004: Message edited by: Jim Yingst ]
Ken Blair
Ranch Hand

Joined: Jul 15, 2003
Posts: 1078
The '12 byte integer' is the length of that record in bytes. I'm absolutely sure it's never going to be too big for 4 bytes, that would have to exceed something like 2 billion wouldn't it? Hell the biggest -file- is about 700 megs, no single record is going to be 2 gig. I'm baffled as to why its 12, but oh well. Thanks it was simpler than I thought, I just insisted on doing everything the hard way.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: writing 4 byte little-endian long integers