Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我有一个短数组,其值介于 0 和短的最大值之间。我缩放数据(将其显示为 TYPE_USHORT),以便生成的短值介于 0 和 65535 之间。我需要打印一些缩放值,但不知道如何打印。数据位于数组和 BufferedImage 中。
最简单的方法是转换为int:
short s = ...; int i = s & 0xffff;
位掩码是为了使转换给出一个 0-65535 范围内的值,而不是 -32768-32767。
从 Java 1.8 开始,同样可以使用Short.toUnsignedInt:
Short.toUnsignedInt
System.out.println("signed s=" + s + ", unsigned s=" + Short.toUnsignedInt(s))