4

**Please check the edit at the bottom of this post
I have a bytebuffer[128 bits] [which has numbers] which I need to convert to bigdecimal, binary, string since these are the corresponding sql mapping while using jdbc.

Is there a library API that I can make use of to do this. I see String.valueof() does not take a byte array as a parameter.So I am stuck to doing something like this:

BigDecimal bd = new BigDecimal(bigmyBuffer.asCharBuffer().toString());

This looks like a hack to me ?. Is there a better way of doing this or rather doing the jdbc part in an efficient manner. I am focused on doing inserts in the respective sql columns as of now.

Edit:
I was wrong , the bytebuffers were not just numbers but all sort of bits. So now I need to take the 128 bit byte buffer and convert it to 2 longs and then merge to a bigdecimal so that the numbers maintain their sanity. So something like this: LongBuffer lbUUID = guid.asLongBuffer();

firstLong=      lbUUID.get();
secondLong =      lbUUID.get();

BigDecimal = firstLong + secondLong ;

Thanks.

codeObserver
  • 6,521
  • 16
  • 76
  • 121

2 Answers2

1

May it is better to detour over BigInteger? Using BigInteger you can interpretate a byte-array as positive large number and from BigInteger it is a very small step to BigDecimal:

byte[] data= new byte[] { 0x12, 0x04, 0x07, 0x05, 0x08, 0x11, 0x38, 0x44, 0x77, 0x33};
BigInteger bi =new BigInteger(1,data);
BigDecimal bd = new BigDecimal(bi);
Robert
  • 39,162
  • 17
  • 99
  • 152
0

Your biggest hurdle is that String ONLY operates with char internally, so you'll need to convert somehow. A slightly cheaper way is

BigDecimal bd = new BigDecimal(new String[bigmyBuffer]);

Since you only have digits, you won't have to worry about charsets here. Unfortunately, this will still create a temporary String object.

The only alternative is to parse the byte buffer manually (i.e. go through it, byte by byte, and initialize your BigDecimal that way) - that will avoid allocating any temporary objects, but ends up in a lot more function calls, so you probably don't want to go that route unless you're really trying to avoid creating that String.

I don't know more about the context of your application, so I'm not sure how exactly your BigDecimal is used.

EboMike
  • 76,846
  • 14
  • 164
  • 167
  • Thnx EboMike .. can you please explain what is the diff in your and my approach .. I am failing to see how it differs internally .. what I think is first byte buffer is converted to chars and then it is wrapped by string object ..does new String[bigmyBuffer] converts bytes to string directly or is it any faster etc ? – codeObserver Dec 03 '10 at 04:22
  • In general, you want to avoid creating objects. In your code, you create a temporary jchararray, and from that you create a temporary String (I'm not sure if the string is able to use the jchararray for its buffer internally - it might). In my example, you'd at least skip one step. – EboMike Dec 03 '10 at 05:59
  • I should qualify that - all this only matters if you do a looooot of these. If not, the difference is minuscule, and it's more important to write code that is understandable and easy to maintain. Again, to really give you a more appropriate answer, I'd need more context on how all this is used. – EboMike Dec 03 '10 at 07:29
  • My approach thows a runtime error [java.lang.NumberFormatException] and yours wont compile [The string class doesnt accept a bytebuffer as constructor argument]. – codeObserver Dec 03 '10 at 22:37
  • What I am doing is inserting this bytebuffer in the numeric column in the database. And I am doing a looot of these inserts more than billion ..its a stress test. – codeObserver Dec 03 '10 at 22:38
  • Oh wait, you have a ByteBuffer, not a byte array? Sorry, I misinterpreted that. What's the possible value range for your integeers? – EboMike Dec 03 '10 at 22:40
  • I added a edit. Turns out that the bits in the byteBuffer are not all numeric so I need to convert them to a numeric like long first. – codeObserver Dec 04 '10 at 02:02
  • What exactly is in the bytebuffer? If it's raw binary data, you can use `BigDecimal(byteBuffer.array())`, and then add those two BigDecimals with add(). – EboMike Dec 04 '10 at 02:09
  • what do you mean when you say "those two" .. (byteBuffer.array()) would just return a bytearray ; also there is no constructor for BigDecimal that takes bytearray as param. Also is there a std implementaion of add() you are mentioning about ? – codeObserver Dec 05 '10 at 00:50