ByteBuffer转换为bigdecimal,binary,string [英] ByteBuffer to bigdecimal, binary, string
问题描述
**请检查此帖子底部的编辑内容
我有一个bytebuffer [128 bits] [其中有数字],我需要将其转换为bigdecimal,二进制,字符串,因为这些是使用jdbc时对应的sql映射.
**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.
是否可以使用库API来执行此操作.我看到String.valueof()并没有将字节数组作为参数,所以我坚持要做这样的事情:
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());
对我来说,这看起来像是骇客吗?有没有更好的方法可以做到这一点,或者更有效地完成jdbc部分.到目前为止,我专注于在各个sql列中进行插入.
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.
我错了,字节缓冲区不仅是数字,而是各种各样的位.因此,现在我需要使用128位字节的缓冲区并将其转换为2个long,然后合并为一个十进制数,以便数字保持其完整性.所以像这样:
LongBuffer lbUUID = guid.asLongBuffer();
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 ;
谢谢.
推荐答案
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);
这篇关于ByteBuffer转换为bigdecimal,binary,string的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!