• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

writing 4 byte little-endian long integers

 
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1078
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Ken Blair
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1078
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1078
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Police line, do not cross. Well, this tiny ad can go through:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic