
本文介绍了在 Java 中将有符号字节数组转换为表示相应无符号值的整数数组的常用方法。由于 Java 中 byte 类型是有符号的,因此需要进行转换以获得无符号表示。本文将探讨使用循环的直接方法,并讨论避免转换的替代方案,例如使用 Byte.toUnsignedInt。
在 Java 中,byte 类型是有符号的,其范围是 -128 到 127。当需要将 byte 值视为无符号值(范围为 0 到 255)时,就需要进行转换。以下是如何将有符号字节数组转换为表示相应无符号值的整数数组的几种方法。
使用循环进行转换
最直接的方法是使用循环遍历字节数组,并将每个 byte 值转换为其对应的无符号 int 值。
public class SignedBytesToUnsignedInts { public static int[] convertBytesToUnsignedInts(byte[] signedBytes) { int[] unsignedInts = new int[signedBytes.length]; for (int i = 0; i < signedBytes.length; i++) { unsignedInts[i] = signedBytes[i] & 0xFF; } return unsignedInts; } public static void main(String[] args) { byte[] signedBytes = {-128, -103, 0, 127}; int[] unsignedInts = convertBytesToUnsignedInts(signedBytes); for (int unsignedInt : unsignedInts) { System.out.println(unsignedInt); } }}
代码解释:
立即学习“Java免费学习笔记(深入)”;
convertBytesToUnsignedInts(byte[] signedBytes) 方法:
接受一个 byte 数组 signedBytes 作为输入。创建一个 int 数组 unsignedInts,其长度与 signedBytes 相同。使用 for 循环遍历 signedBytes 数组。在循环中,使用按位与运算符 & 将每个 byte 值与 0xFF (255) 进行与运算。 这个操作会将 byte 扩展到 int,同时保留其低 8 位,从而有效地将其转换为无符号 int。将结果存储在 unsignedInts 数组的相应位置。返回 unsignedInts 数组。
main(String[] args) 方法:
创建一个示例 byte 数组 signedBytes,包含一些有符号的字节值。调用 convertBytesToUnsignedInts 方法将 signedBytes 转换为 unsignedInts。使用增强型 for 循环遍历 unsignedInts 数组,并打印每个无符号整数值。
注意事项:
& 0xFF 操作是关键。它确保 byte 值被视为无符号值。由于 Java 的 byte 是有符号的,直接将其赋值给 int 会进行符号扩展,导致负值。& 0xFF 清除了高位的符号扩展,只保留低 8 位。此方法效率很高,因为它只涉及简单的循环和按位运算。
使用 Byte.toUnsignedInt() (Java 8+)
从 Java 8 开始,Byte 类提供了一个方便的 toUnsignedInt() 方法,可以直接将 byte 转换为无符号 int。
import java.util.Arrays;public class SignedBytesToUnsignedInts { public static int[] convertBytesToUnsignedInts(byte[] signedBytes) { return Arrays.stream(signedBytes) .mapToInt(Byte::toUnsignedInt) .toArray(); } public static void main(String[] args) { byte[] signedBytes = {-128, -103, 0, 127}; int[] unsignedInts = convertBytesToUnsignedInts(signedBytes); for (int unsignedInt : unsignedInts) { System.out.println(unsignedInt); } }}
代码解释:
立即学习“Java免费学习笔记(深入)”;
convertBytesToUnsignedInts(byte[] signedBytes) 方法:
使用 Arrays.stream(signedBytes) 创建一个 byte 数组的流。使用 mapToInt(Byte::toUnsignedInt) 将流中的每个 byte 值转换为无符号 int 值。Byte::toUnsignedInt 是一个方法引用,它等价于 b -> Byte.toUnsignedInt(b)。使用 toArray() 将流转换为 int 数组。返回 int 数组。
main(String[] args) 方法:
创建一个示例 byte 数组 signedBytes,包含一些有符号的字节值。调用 convertBytesToUnsignedInts 方法将 signedBytes 转换为 unsignedInts。使用增强型 for 循环遍历 unsignedInts 数组,并打印每个无符号整数值。
注意事项:
这种方法使用 Java 8 的流 API,代码更简洁易读。在性能方面,循环通常比流略快,但流的优势在于可读性。
避免转换
在某些情况下,可以避免完全转换字节数组。例如,如果只是想将单个字节视为无符号值,可以使用 Byte.toUnsignedInt() 方法或 & 0xFF 操作符直接进行操作。
public class SignedBytesToUnsignedInts { public static void main(String[] args) { byte signedByte = -128; // 使用 Byte.toUnsignedInt() int unsignedInt1 = Byte.toUnsignedInt(signedByte); System.out.println("Byte.toUnsignedInt: " + unsignedInt1); // 使用 & 0xFF int unsignedInt2 = signedByte & 0xFF; System.out.println("& 0xFF: " + unsignedInt2); }}
代码解释:
立即学习“Java免费学习笔记(深入)”;
main(String[] args) 方法:创建一个示例 byte 变量 signedByte,包含一个有符号的字节值。使用 Byte.toUnsignedInt() 方法将 signedByte 转换为无符号 int 值,并打印结果。使用 & 0xFF 操作符将 signedByte 转换为无符号 int 值,并打印结果。
结论
将有符号字节数组转换为无符号整数数组有多种方法。选择哪种方法取决于具体的需求和 Java 版本。对于 Java 8 及以上版本,Byte.toUnsignedInt() 方法提供了一种简洁易读的方式。对于更早的版本,可以使用循环和按位与运算符。如果可能,尽量避免完全转换数组,而是直接将单个字节视为无符号值进行操作。
以上就是将有符号字节数组转换为无符号整数数组的 Java 方法的详细内容,更多请关注创想鸟其它相关文章!
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/59105.html
微信扫一扫
支付宝扫一扫