• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

Read 16 byte data from ByteBuffer

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am able to read 4byte data using byteBuf.getInt() and same way till 8 bytes I am using getLong.

I am not able to read 16 byte data. when I googled I understand that java BigDecimal will support 16 byte data.

Could any one please help on reading 16 byte data from ByteBuffer?

I am not sure how to proceed further.

 
author
Posts: 23928
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

peral king wrote:I am able to read 4byte data using byteBuf.getInt() and same way till 8 bytes I am using getLong.

I am not able to read 16 byte data. when I googled I understand that java BigDecimal will support 16 byte data.



First, welcome to the ranch !!

The BigDecimal class supports big numbers -- but that doesn't mean that it uses a 16 byte representation. Instances of BigDecimal are objects, and not primitive data types.

peral king wrote:
Could any one please help on reading 16 byte data from ByteBuffer?



The ByteBuffer getLong() method simply extracts 8 bytes into a primitive long, and deal with the Endian correctly.

You can do the same for 16 bytes by dealing with it in smaller chunks. You can then calculate the value into a BigDecimal (or BigInteger) result, which will hold the value (although it is no longer a 16 byte representation)... and of course, how you calculate the result will depend on the Endian, so that is also now your responsibility too.


Now... Your next question is likely "how?" ... which unfortunately, requires knowledge of Big/Little Endian, and the Twos Complement format. It will be difficult having that discussion until you understand those two topics.

Henry

 
Marshal
Posts: 76395
364
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

I do not believe you can use a byte[] to create a Big Decimal. Maybe you meant BigInteger, which does have a constructor taking byte[].
 
Henry Wong
author
Posts: 23928
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:
I do not believe you can use a byte[] to create a Big Decimal. Maybe you meant BigInteger, which does have a constructor taking byte[].



Cool. I didn't know that the BigInteger has that functionality built in.

Of course, it does assume Big Endian -- which since that is the network byte order, and the endian used by Java, should likely, be the endian that you want to handle (otherwise, you have to take care it it... )

Henry
 
peral king
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Guys for your response.

I tried as below still its not working (please bear with me as I am completely New to Java)






 
Campbell Ritchie
Marshal
Posts: 76395
364
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why are you using littleendian when Henry has told you big integer requires bigendian order?
 
Henry Wong
author
Posts: 23928
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Why are you using littleendian when Henry has told you big integer requires bigendian order?



The OP never mentioned whether the data was in little or big endian ... until now. I guess since it is little endian, the BigInteger constructor can't be directly used.

Henry
 
Henry Wong
author
Posts: 23928
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

peral king wrote:
I tried as below still its not working (please bear with me as I am completely New to Java)



Some issues...

1. Adding two long variables (using a long) can overflow, and hence, produce data loss. Remember that the value is 16 bytes long, so you need to store it with something that can hold that value... perhaps with a BigInteger?

2. Adding two long variables is *not* how to deal with little endian. With little endian, the low order long word is first, so you need to shift the high order long word (by 64 bits) before adding it to the low order long word.

3. And you still need to deal with the sign bit. Remember that primitive longs are signed, so it is possible that the low (and the high) long words may be negative. You may have to convert it to a positive (ie. unsigned value) before dealing with the endian.

If this sounds complex, perhaps it is. In fact, I don't even know where to give you a hint... except perhaps to go do some research on endian-ness, and the twos complement format.


Anyway, as Campbell mentioned, since the BigInteger class has support for this (but in Big Endian only), perhaps it is easier to convert the little endian to big endian, and then use the constructor -- no need to understand twos complement.

Henry
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic