是否存在用于无符号数字类型包装器的Java库? [英] Is there a Java library for unsigned number type wrappers?

查看:123
本文介绍了是否存在用于无符号数字类型包装器的Java库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

显然,Java本身不支持无符号数字类型,那不是即将改变(评论始于2002年)。但是,在使用MySQL等数据库时,它们可能会偶尔派上用场。有很多问题涉及如何模拟无符号数。例如:

Obviously, Java doesn't support unsigned number types natively, and that's not going to change soon (comments starting in 2002). However, when working with databases, such as MySQL, they may come in handy every now and then. There are a lot of questions dealing with how to simulate unsigned numbers. For example:

  • unsigned short in java
  • Java: Unsigned numbers
  • Understanding Java unsigned numbers

所有这些都表面描述可以完成的方式。但是有没有任何库实际上一直在为 UByte UShort UInteger , ULong ?优选地,那些包装器将扩展 java.lang.Number 并提供类似于 java.math.BigInteger的算术API

All of them superficially describe how it could be done. But is there any library actually going all the way and implementing suitable wrappers for UByte, UShort, UInteger, ULong? Preferably, those wrappers would extend java.lang.Number and provide an arithmetic API similar to that of java.math.BigInteger.

此文件中可以看到,有很多想法,还有很多可能出错的地方(例如如何按位移位,如何乘法等),所以我不想自己做。此外,我不想只使用下一个更高的类型(例如而不是字节等) 。我想要一个 8位的概念, 16位 32位<保留数字/ code>, 64位数字,以便与数据库进行最佳交互。

As can be seen in this document, there's a lot to think of, and a lot that can go wrong (e.g. how to bitwise shift, how to multiply, etc), so I don't want to do it myself. Also, I don't want to just use the next higher type (e.g. Short instead of Byte, etc.). I want the notion of an 8-bit, 16-bit, 32-bit, 64-bit number preserved, for best interaction with databases, for instance.

更新

在您回答之前!考虑到我知道所有的解决方法,但我真的很想拥有具有上述属性的4种类型。也许有人已经这样做了,所以这就是我问的原因。无需提醒我解决方法。

Before you answer! Consider that I know all the workarounds, but I'd really really like to have exactly those 4 types with exactly the above properties. And maybe someone has already done that, so that's why I ask. No need to remind me of the workarounds.

推荐答案

当我在 jOOQ ,我还没有找到类似的东西,所以我推出了自己的开源库,我称之为jOOU(U代表无符号):

When I needed this functionality inside of jOOQ, I haven't found anything like it, so I rolled my own Open Source library that I call jOOU (U for Unsigned):

http://github.com/jOOQ/jOOU

我知道有些人可能认为这有点矫枉过正,但我​​真的很想要包装其他语言所称的包装器 ubyte ushort uint ulong 。希望与Valhalla一起,这些包装器可以转换为值类型。

I understand that some may think this is overkill, but I'd really like to have precisely those wrappers wrapping what other languages call ubyte, ushort, uint, ulong. Hopefully with Valhalla, those wrappers can be turned into value types.

当然,非常欢迎对算术/按位操作实现的贡献!

Of course, contributions to the arithmetics / bitwise operation implementations are very welcome!

这篇关于是否存在用于无符号数字类型包装器的Java库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆