如何在 Java 中将短格式打印为无符号短格式

发布于 2024-09-07 13:30:42 字数 139 浏览 4 评论 0原文

我有一个 Short 数组,其值范围在 0 到 Short 的最大值之间。我缩放数据(将其显示为 TYPE_USHORT),以便生成的短值范围在 0 到 65535 之间。我需要打印一些缩放后的值,但不知道如何打印。数据位于数组和 BufferedImage 中。

I have an array of short whose values range between 0 and the maximum value of a short. I scale the data (to display it as TYPE_USHORT) so that the resulting short values range between 0 and 65535. I need to print some of the scaled values but can't figure out how. The data are in an array and in a BufferedImage.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

小苏打饼 2024-09-14 13:30:42

最简单的方法是转换为 int:

short s = ...;
int i = s & 0xffff;

位掩码是使转换给出 0-65535 范围内的值,而不是 -32768-32767。

The simplest way is to convert to int:

short s = ...;
int i = s & 0xffff;

The bitmask is to make the conversion give a value in the range 0-65535 rather than -32768-32767.

旧人哭 2024-09-14 13:30:42

从 Java 1.8 开始,可以使用 Short.toUnsignedInt 完成相同的操作:

System.out.println("signed s=" + s + ", unsigned s=" + Short.toUnsignedInt(s))

Since Java 1.8, the same can be done with Short.toUnsignedInt:

System.out.println("signed s=" + s + ", unsigned s=" + Short.toUnsignedInt(s))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文