ByteBuffer for bigdecimal, binary, string

** Please check the changes at the bottom of this post
I have a bytebuffer [128 bit] [which has numbers] that I need to convert to bigdecimal, binary, string, since this is the corresponding sql mapping when using jdbc.

Is there a library API that I can use for this. I see that String.valueof () does not accept a byte array as a parameter. Therefore, I adhere to doing something like this:

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

It looks like hacking me. Is there a better way to do this, or rather make the jdbc part efficiently. I am focused on doing insertions in the corresponding sql columns at the moment.

Edit:
I was wrong, the bytebuffers were not just numbers, but all kinds of cue ball. So now I need to take a 128-bit buffer and convert it to 2 longs, and then merge it into bigdecimal so that the numbers keep common sense. So something like this: LongBuffer lbUUID = guid.asLongBuffer ();

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

BigDecimal = firstLong + secondLong ;

Thank.

+3
source share
2 answers

Could it be better to bypass BigInteger ? Using BigInteger, you can interpret the byte array as a 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);
+1
source

, String char , - .

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

, . , String.

, (.. , BigDecimal) - - , , , , , .

, , BigDecimal.

0

Source: https://habr.com/ru/post/1778221/


All Articles